7

I use following code to put a checkbox in the 9th column of my QTableWidget

chkBoxItem = QtGui.QTableWidgetItem()
chkBoxItem.setFlags(QtCore.Qt.ItemIsUserCheckable | QtCore.Qt.ItemIsEnabled)
chkBoxItem.setCheckState(QtCore.Qt.Unchecked)       
table.setItem(rowNo,9,chkBoxItem)

Where table is my QtTableWidget. I need to add the row where the checkbox is clicked to a list.. how on earth do I achieve this?

Kind regards,

2
  • Have you tried to just make a list attribute storing the states for each row ? Or a property? Or are you having problem connecting the clicked signal of you chkBoxItem to a proper method? Commented Sep 11, 2012 at 12:35
  • check this post stackoverflow.com/questions/39511181/… Commented May 25, 2021 at 17:21

1 Answer 1

16

One way to do it would be:

  1. connect the itemClicked signal of the table to a handler

  2. test the checkedState of the clicked item in the handler

  3. if the item is checked, add its row to the list

Example code:

PyQt5/6:

from PyQt5 import QtCore, QtWidgets
# from PyQt6 import QtCore, QtWidgets

class Window(QtWidgets.QWidget):
    def __init__(self, rows, columns):
        super().__init__()
        self.table = QtWidgets.QTableWidget(rows, columns, self)
        for column in range(columns):
            for row in range(rows):
                item = QtWidgets.QTableWidgetItem('Text%d' % row)
                if row % 2:
                    item.setFlags(QtCore.Qt.ItemFlag.ItemIsUserCheckable |
                                  QtCore.Qt.ItemFlag.ItemIsEnabled)
                    item.setCheckState(QtCore.Qt.CheckState.Unchecked)
                self.table.setItem(row, column, item)
        self.table.itemClicked.connect(self.handleItemClicked)
        layout = QtWidgets.QVBoxLayout(self)
        layout.addWidget(self.table)
        self._list = []

    def handleItemClicked(self, item):
        if item.checkState() == QtCore.Qt.CheckState.Checked:
            print('"%s" Checked' % item.text())
            self._list.append(item.row())
            print(self._list)
        else:
            print('"%s" Clicked' % item.text())

if __name__ == '__main__':

    import sys
    app = QtWidgets.QApplication(sys.argv)
    window = Window(6, 3)
    window.resize(350, 300)
    window.show()
    sys.exit(app.exec())

PyQt4:

from PyQt4 import QtGui, QtCore

class Window(QtGui.QWidget):
    def __init__(self, rows, columns):
        QtGui.QWidget.__init__(self)
        self.table = QtGui.QTableWidget(rows, columns, self)
        for column in range(columns):
            for row in range(rows):
                item = QtGui.QTableWidgetItem('Text%d' % row)
                if row % 2:
                    item.setFlags(QtCore.Qt.ItemIsUserCheckable |
                                  QtCore.Qt.ItemIsEnabled)
                    item.setCheckState(QtCore.Qt.Unchecked)
                self.table.setItem(row, column, item)
        self.table.itemClicked.connect(self.handleItemClicked)
        layout = QtGui.QVBoxLayout(self)
        layout.addWidget(self.table)
        self._list = []

    def handleItemClicked(self, item):
        if item.checkState() == QtCore.Qt.Checked:
            print('"%s" Checked' % item.text())
            self._list.append(item.row())
            print(self._list)
        else:
            print('"%s" Clicked' % item.text())

if __name__ == '__main__':

    import sys
    app = QtGui.QApplication(sys.argv)
    window = Window(6, 3)
    window.resize(350, 300)
    window.show()
    sys.exit(app.exec_())
Sign up to request clarification or add additional context in comments.

1 Comment

if there is button say select all then how can we implement it ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.