Search code examples
pythonpython-3.xpyqtpyqt5qtablewidget

Determine if a cell is empty in table widget


I am having issues with tablewidget.

Issue

I am getting the input from the user in a table widget (5 rows and 1 column). It is not necessary for the user to fill all the rows. Let’s say for instance, he fills only 3 of them.

Then I am trying to read those rows in python using:

tableWidget.item(1,1).text()

But if I leave some rows empty, there is always some problem running the code.

So can anyone tell me how to check if a cell is empty and I can skip reading it?

At the moment I am using two following possibilities to check that thing:

1) Basic Logic

if len(tableWidget.item(4,1).text())!=0:
    print('Cell is non-empty')

2) From Qt documentation

if tableWidget.item(4,1).text().isEmpty():
    print('Cell is empty')

But none of them work! Thanks a lot!

Code (click_me)

from PyQt5 import QtCore, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.tableWidget = QtWidgets.QTableWidget(self.centralwidget)
        self.tableWidget.setGeometry(QtCore.QRect(120, 70, 256, 192))
        self.tableWidget.setRowCount(5)
        self.tableWidget.setObjectName("tableWidget")
        self.tableWidget.setColumnCount(1)
        item = QtWidgets.QTableWidgetItem()
        item.setText("Selected File")
        self.tableWidget.setHorizontalHeaderItem(0, item)
        self.pushButton_ok = QtWidgets.QPushButton("OK",self.centralwidget)
        self.pushButton_ok.setGeometry(QtCore.QRect(100, 290, 337, 23))
        self.pushButton_ok.setObjectName("pushButton_ok")
        MainWindow.setCentralWidget(self.centralwidget)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

        self.pushButton_ok.clicked.connect(lambda:self.click_me())

    def click_me(self):
        rows  =  self.tableWidget.rowCount()
        listi = []
        for i in range(0,rows):
            jar = self.tableWidget.item(i,0).text()
            if len(jar)!=0:
                listi.append(jar)

if __name__ == "__main__":
    import sys
    app = QtCore.QCoreApplication.instance()
    if app is None:
        app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    app.exec_()

Solution

  • First of all that you have created a QTableWidget and then you have established the number of rows and columns does not imply that for each box there is a QTableWidgetItem, these will only be created if you create them explicitly or through the editing that the user does, this is done by performance issues.

    So an empty box may indicate that this box does not have an associated QTableWidgetItem or that the QTableWidgetItem text is empty. So you have to check both, so the correct thing would be to verify first the existence of the item, and then if the item has text:

    def click_me(self):
        rows  =  self.tableWidget.rowCount()
        listi = []
        for i in range(rows):
            it = self.tableWidget.item(i, 0)
            if it:
                if it.text():
                    listi.append(it.text())
        print(listi)
    

    Or taking advantage of the fact that in Python the Boolean statements are verified from left to right, a more compact form can be obtained:

    def click_me(self):
        rows  =  self.tableWidget.rowCount()
        listi = []
        for i in range(rows):
            it = self.tableWidget.item(i, 0)
            if it and it.text():
                listi.append(it.text())