Search code examples
pythonpyqtpysideqtablewidgetqtablewidgetitem

Inserting multiple QCheckBox into QTableWidget odd rows


I'm trying to create a table with 160 Rows, and then inserting a QCheckBox every odd number of rows, specifically on column 10. The problem is, i have to create 80 QCheckBox (one for each row, so they can be separately assigned by the user)...

Creating one by one the 80 QCheckBox objects for the 9 projects I have to do is simply nonsense!

Is there any way of doing that by a loop? I can't think of anything, I searched for the answer and found nothing.

[...]
# importing PySide
from PySide import QtGui, QtCore
[...]
# Creating a Table
class Table(QtGui.QDialog):
    def __init__(self, parent=None):
        super(Table, self).__init__(parent)
        self.table = QtGui.QTableWidget()
        self.table.setRowCount(160)
        self.table.setColumnCount(10)
# This is the tricky part:
        chkBoxItem = QtGui.QTableWidgetItem()
        chkBoxItem.setFlags(QtCore.Qt.ItemIsUserCheckable|QtCore.Qt.ItemIsEnabled)
        chkBoxItem.setCheckState(QtCore.Qt.Unchecked)

        chkBoxItem2 = QtGui.QTableWidgetItem()
        chkBoxItem2.setFlags(QtCore.Qt.ItemIsUserCheckable|QtCore.Qt.ItemIsEnabled)
        chkBoxItem2.setCheckState(QtCore.Qt.Unchecked)

        chkBoxItem3 = QtGui.QTableWidgetItem()
        chkBoxItem3.setFlags(QtCore.Qt.ItemIsUserCheckable|QtCore.Qt.ItemIsEnabled)
        chkBoxItem3.setCheckState(QtCore.Qt.Unchecked)
[...]
# Then insert all of them in the Table:
        self.table.setItem(0, 10, chkBoxItem)
        self.table.setItem(2, 10, chkBoxItem2)
        self.table.setItem(4, 10, chkBoxItem3)
        self.table.setItem(6, 10, chkBoxItem4)
        self.table.setItem(8, 10, chkBoxItem5)
        self.table.setItem(10, 10, chkBoxItem6)
        self.table.setItem(12, 10, chkBoxItem7)
[...]

Solution

  • This basic script creates an UI containing a 160*10 QTable and a QPushButton. Every odd row, a checkbox is added in the cell of the 10th column. Clicking on the button displays a list of the state of all checkboxes.

    States:

    • 0: Unchecked
    • 2: Checked
    • There is a state 1 but I don't remember what it is used for, I'll check the docs.

    Note:

    This has been made using PyQt

    Code:

    import math, sys
    from PyQt4.QtCore import Qt, QTimer
    from PyQt4.QtGui import *
    
    class MainWindow(QMainWindow):
        def __init__(self, parent = None):
            QMainWindow.__init__(self, parent)
    
            #Create Basic UI
            self.mainWidget = QWidget(self)
            self.table = QTableWidget()
            self.table.setRowCount(160)
            self.table.setColumnCount(10)
    
            self.button = QPushButton("Print stuff")
    
            layout = QVBoxLayout(self.mainWidget)
            layout.addWidget(self.table)
            layout.addWidget(self.button)
    
            self.setCentralWidget(self.mainWidget)
    
            self.button.clicked.connect(self.printStuff)
            #################
    
            #Fill the table 
            self.rowRange = range(0, self.table.rowCount(), 2)
            for i in self.rowRange:
                chkBoxItem = QTableWidgetItem()
                chkBoxItem.setFlags(Qt.ItemIsUserCheckable|Qt.ItemIsEnabled)
                chkBoxItem.setCheckState(Qt.Unchecked)
                self.table.setItem(i, 9, chkBoxItem)
            ###############
    
        def printStuff(self): #You can remove this, this is for testing purpose only
            print [(i+1, self.table.item(i, 9).checkState()) for i in self.rowRange]
    
    if __name__ == "__main__":
    
        app = QApplication(sys.argv)
        window = MainWindow()
        window.show()
        sys.exit(app.exec_())