Search code examples
pythonpyqtpyqt5qtablewidgetqtablewidgetitem

Is there a way to check which check boxes have been checked in a PyQt5 table


I'm attempting to make a room booking system, using a table. In the table I have check boxes, I would like it so that when the user clicks the book button, the program would see which check boxes have been checked, so that it can remove those check boxes from the table.

I have created the table through PyQt5 designer, with checkboxes in each space of the table like this:

enter image description here

The UI file code has many lines like this:

        item = QtWidgets.QTableWidgetItem()
        item.setFlags(QtCore.Qt.ItemIsUserCheckable|QtCore.Qt.ItemIsEnabled)
        item.setCheckState(QtCore.Qt.Unchecked)
        self.tableWidget.setItem(0, 0, item)
        item = QtWidgets.QTableWidgetItem()
        item.setFlags(QtCore.Qt.ItemIsUserCheckable|QtCore.Qt.ItemIsEnabled)
        item.setCheckState(QtCore.Qt.Unchecked)
        self.tableWidget.setItem(0, 1, item)

Does this mean that the check boxes are all called item? is there a way in which I can a separate name for each check boxes?

Sorry if this seems like a basic question - Thanks in advance


Solution

  • You have to iterate over the table and do a check of the status of each item, using the filtered items you can get the row and column with what you can get the labels.

    from PyQt5 import QtCore, QtGui, QtWidgets
    
    class Widget(QtWidgets.QWidget):
        def __init__(self, parent=None):
            super(Widget, self).__init__(parent)
            self.tableWidget = QtWidgets.QTableWidget(6, 5)
            self.book_button = QtWidgets.QPushButton("Book")
            self.book_button.clicked.connect(self.book_clicked)
    
            lay = QtWidgets.QVBoxLayout(self)
            lay.addWidget(self.tableWidget)
            lay.addWidget(self.book_button)
    
            self.tableWidget.setHorizontalHeaderLabels("P1 P2 P3 P4 P5 P6".split())
            self.tableWidget.setVerticalHeaderLabels("C101 C214 C320 F04 E201".split())
    
            for i in range(self.tableWidget.rowCount()):
                for j in range(self.tableWidget.columnCount()):
                    item = QtWidgets.QTableWidgetItem()
                    item.setFlags(item.flags() | QtCore.Qt.ItemIsUserCheckable)
                    item.setCheckState(QtCore.Qt.Unchecked)
                    self.tableWidget.setItem(i, j, item)
    
        @QtCore.pyqtSlot()
        def book_clicked(self):
            items = []
            for i in range(self.tableWidget.rowCount()):
                for j in range(self.tableWidget.columnCount()):
                    item = self.tableWidget.item(i, j)
                    if item.checkState() == QtCore.Qt.Checked:
                        items.append(item)
    
            for it in items:
                r = it.row()
                c = it.column()
                v, h = self.tableWidget.horizontalHeaderItem(c).text(), self.tableWidget.verticalHeaderItem(r).text()
                print(h, v)
    
    if __name__ == '__main__':
        import sys
        app = QtWidgets.QApplication(sys.argv)
        w = Widget()
        w.show()
        sys.exit(app.exec_())