Home > Mobile >  How to print row values from their corresponding buttons in a QTableWidget?
How to print row values from their corresponding buttons in a QTableWidget?

Time:12-06

Apologies if the title is not clear.

Brief: I'm using PYQT5 to create a table using QTableWidget. To pull data from my server and populate it on the table, I'm using a JSON API. Along with that, I'm inserting a button called "Acknowledge" at the end of every row.

Objective: What I wish to do is every time I click the "Acknowledge" button (shown in the above pic), it should call JSON value from the same API and then print it.

An idea of the API I'm using (Sorry can't post the real data): [{"Zone":"Zone1","Room":"Room1","Device":"10","date_added":"2022-10-02 01:45:45","data_id":"120"},{"Zone":" Zone2","Room":" Room2","Device":"11","date_added":"2022-11-02 01:19:29","data_id":"121"},{"Zone":" Zone1","Room":" Room1","Device":"12","date_added":"2022-12-02 07:19:11","data_id":"122"}]

Code to insert data and button into the table:


    def __init__(self):
         self.dictAck = {}

    def latestdata(self):

        req = requests.get('...') #JSON API

        response = req.json()
        my_data_str = json.dumps(response)
        dataResponse = json.loads(my_data_str)
        row=0       

        self.latestdatatable.setRowCount(len(dataResponse))

        for item in dataResponse:
            self.my_buttons.append(None)
            self.ack_btn = QtWidgets.QPushButton("Acknowledge")
            self.ack_btn.setGeometry(QtCore.QRect(500, 290, 112, 34))
            self.ack_btn.setStyleSheet("border: none;")
            self.ack_btn.setText("")
            icon7 = QtGui.QIcon()
            icon7.addPixmap(QtGui.QPixmap("images/check.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
            self.ack_btn.setIcon(icon7)
            self.ack_btn.setIconSize(QtCore.QSize(40, 35))
            self.latestdatatable.setItem(row,0,QtWidgets.QTableWidgetItem(str(item["date_added"])))
            self.latestdatatable.setItem(row,1,QtWidgets.QTableWidgetItem(str(item["Zone"])))
            self.latestdatatable.setItem(row,2,QtWidgets.QTableWidgetItem(str(item["Room"])))
            self.latestdatatable.setItem(row,3,QtWidgets.QTableWidgetItem(str(item["Device"])))
            self.latestdatatable.setCellWidget(row, 4, self.ack_btn)

            self.dictAck[self.ack_btn] = (row,item["data_id"])
            self.ack_btn.clicked.connect(self.acknowledge)
            
            row=row 1

The below function is called when the button is clicked. This is the function I've implemented to print the JSON value. I need to output the value of the data_id key.

    def acknowledge(self):
        req = requests.get('...') #JSON API

        response = req.json()
        my_data_str = json.dumps(response)
        dataResponse = json.loads(my_data_str)
        row=0       

        self.latestdatatable.setRowCount(len(dataResponse))

        for item in dataResponse:
            data_id = (str(item["data_id"]))
            print(data_id);

Problem: Unfortunately the code above is printing all values after clicking any 1 button instead of the only value I wish to print from the 1 button I clicked from its corresponding row.

Undesirable Result (After clicking the first button, for example): 120 121 122

Expected Result (After clicking the first button, for example): 120

Apologies for asking the question in a complex manner.

CodePudding user response:

You should pass arguments when define connect signal like that:

self.ack_btn.clicked.connect(lambda: self.acknowledge(data_id))

And define acknowledge funciton with data_id argument:

def acknowledge(self, data_id):
    print(data_id)
  • Related