Glazbee
Glazbee

Reputation: 648

Retrieving the contents of a QtTableWidget cell from PyQT5

I have some data that I'd like to be select in a QtTableWidget. I want to be able to select a specific row of the table, and execute a function based on the selected row. The column isn't important.

Here's a sample of the data I'm using

Id | Test name    | Owner | Script source
---|-----------   |-------|--------------
 1 | Login        |  1    | test_login_s
 2 | Logout       |  1    | test_logout_s
 3 | User > Edit  |  1    | test_user_edit_s

All of the following code is executed from this line in retranslateUI

self.btnRun.clicked.connect(self.run_selected_test)

Initially, I tried the following method:

row = self.tblTable.currentRow()
cell = self.tblTable.itemAt(row, 3).text()

This returns 1, the cell at 0,0 regardless of which cell is selected. Looking at the QTableWidget class reference I noticed that itemAt takes two integers and returns 0 if the specified point is not covered by an item in the table widget.

Following that, I amended the code slightly to:

cell = self.tblTable.itemAt(1, 1).text()

That did not fix the issue.


To test this further, I decided to check if currentRow and currentColumn are updating as intended.

row = self.tblTable.currentRow()
> 4
> 17

column = self.tblTable.currentColumn()
> 0
> 6

I have been able to create a workaround like so

    if row == 0:
        do_stuff()
    elif row == 1:
        do_stuff()
    elif row == 2:
        do_stuff()
    elif row == 3:

The issue with this method is there are over 80 rows. This seems inefficient for anything greater than 5.

Upvotes: 1

Views: 4522

Answers (1)

S. Nick
S. Nick

Reputation: 13681

QTableWidgetItem *QTableWidget::item(int row, int column) const

Returns the item for the given row and column if one has been set; otherwise returns 0.

Try it:

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui  import *
from PyQt5.QtCore import *

class Example(QMainWindow):
    def __init__(self, data):
        super().__init__()
        self.data = data
        self.cols = len(self.data[0])
        self.row, self.col = 0, 0
        self.cell = []

        centralWidget = QWidget()
        self.setCentralWidget(centralWidget)

        self.label = QLabel(alignment=Qt.AlignCenter)

        self.tableWidget = QTableWidget(0, 4)
        self.tableWidget.setHorizontalHeaderLabels(["Id", "Test name", "Owner", "Script source"])
        self.tableWidget.cellClicked.connect(self.cellClick)
        self.setTableWidget()

        self.btnRun = QPushButton("btnRun")
        self.btnRun.clicked.connect(self.run_selected_test)

        lay = QGridLayout(centralWidget)
        lay.addWidget(self.label, 0, 0)
        lay.addWidget(self.tableWidget, 1, 0)  
        lay.addWidget(self.btnRun, 2, 0) 

    def run_selected_test(self):
        self.cell = []
        for col in range(self.cols):
            self.cell.append(self.tableWidget.item(self.row, col).text())        # <---
        self.label.setText("{}".format(" {}  ; "*self.cols).format(*self.cell))

    def cellClick(self, row, col):
        self.row = row
        self.col = col

    def setTableWidget(self):    
        for r, (_id, _name, _owner, _script) in enumerate(self.data):
            it_id     = QTableWidgetItem(_id)
            it_name   = QTableWidgetItem(_name)
            it_owner  = QTableWidgetItem(_owner)
            it_script = QTableWidgetItem(_script)

            self.tableWidget.insertRow(self.tableWidget.rowCount())
            for c, item in enumerate((it_id, it_name, it_owner, it_script)):
                self.tableWidget.setItem(r, c, item)


data = [("1", "Login",       "1", "test_login_s"), 
        ("2", "Logout",      "1", "test_logout_s"), 
        ("3", "User > Edit", "1", "test_user_edit_s")]

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Example(data)
    ex.setGeometry(300, 150, 450, 200)
    ex.show()
    sys.exit(app.exec_())

enter image description here

Upvotes: 1

Related Questions