Search code examples
pythonpyqtpyqt4pyqt5qt-designer

how to add checkbox in each row of QTableView in PYQT


I have a simple .xlsx file and I load its content to my qtableview. My code likes this:

import xlrd
from PyQt5 import QtCore, QtGui, QtWidgets, uic

form_class = uic.loadUiType("SearchView.ui")[0]  # Load the UI

app = None
myWindow = None
dic_messages = dict()


class MyWindowClass(QtWidgets.QMainWindow, form_class):
    def __init__(self, fileName, parent=None):
        QtWidgets.QMainWindow.__init__(self, parent)
        self.setupUi(self)
        self.gui = form_class
        self.fileName = fileName
        self.model = QtGui.QStandardItemModel(self)
        self.tableView.setModel(self.model)
        self.fn_load_messages()

    def fn_load_messages(self):
        workbook = xlrd.open_workbook('sample.xlsx')
        worksheet = workbook.sheet_by_name('sample_sheet')

        for i in range(0, worksheet.nrows):
            dic_messages[i + 1] = (worksheet.cell(i, 0).value, worksheet.cell(i, 1).value, worksheet.cell(i, 2).value, worksheet.cell(i, 3).value, worksheet.cell(i, 4).value)
            i += 1

        rowPosition = self.tbl_messages.rowCount()
        self.tbl_messages.insertRow(rowPosition)

      return

def main():
    global app, myWindow
    app = QtWidgets.QApplication(sys.argv)
    myWindow = MyWindowClass('sample.xlsx')
    myWindow.show()
    app.exec_()


if __name__ == '__main__':
    main()

i want to add a checkbox column in each row and then i want to check or unchecked each row i want. i added below part to my code but the checkboxes are not active:

self.tableView.setItemDelegateForColumn(0 , CheckBoxDelegate(self))
class CheckBoxDelegate(QtWidgets.QStyledItemDelegate):
 def paint(self, painter, option, index):
    check_box_style_option = QtWidgets.QStyleOptionButton()
    check_box_style_option.rect = self.getCheckBoxRect(option)
    check_box_style_option.state = QtWidgets.QStyle.State_Enabled
    QtWidgets.QApplication.style().drawControl(QtWidgets.QStyle.CE_CheckBox, 
    check_box_style_option, painter)

def getCheckBoxRect(self, option):
    check_box_style_option = QtWidgets.QStyleOptionButton()
    check_box_rect = QtWidgets.QApplication.style().subElementRect(QtWidgets.QStyle.SE_CheckBoxIndicator, check_box_style_option, None)
    check_box_point = QtCore.QPoint (option.rect.x() +
                        option.rect.width() / 2 -
                        check_box_rect.width() / 2,
                        option.rect.y() +
                        option.rect.height() / 2 -
                        check_box_rect.height() / 2)
    return QtCore.QRect(check_box_point, check_box_rect.size())

how can I do this? Thanks.


Solution

  • You don't set it in the view but in the items and model. Like this:

    #!/usr/bin/env python
    
    from PyQt5 import QtWidgets, QtGui
    
    class MyWidget(QtWidgets.QWidget):
    
        def __init__(self, parent=None):
            super().__init__(parent=parent)
    
            self.tableModel = QtGui.QStandardItemModel(self)
            self.tableModel.itemChanged.connect(self.itemChanged)
    
            item = QtGui.QStandardItem("Click me")
            item.setCheckable(True)
            self.tableModel.appendRow(item)
    
            self.mainLayout = QtWidgets.QVBoxLayout()
            self.setLayout(self.mainLayout)
    
            self.tableView = QtWidgets.QTableView()
            self.tableView.setModel(self.tableModel)
            self.mainLayout.addWidget(self.tableView)
    
        def itemChanged(self, item):
            print("Item {!r} checkState: {}".format(item.text(), item.checkState()))
    
    
    def main():
        app = QtWidgets.QApplication([])
    
        win = MyWidget()
        win.show()
        win.raise_()
        app.exec_()
    
    if __name__ == "__main__":
        main()