PyQt allign checkbox and put it in every row

前端 未结 2 448
清歌不尽
清歌不尽 2021-01-16 11:48

I\'m trying to do this with the check-box. Sadly is made for C++ and any adaptation of the code for Python has the result this error: \'QWidget\' object is not callabl

2条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-16 11:54

    Here is an example from ekhumoro to find what is checked when it s being clicked :

    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_())
    

    But you can also iterate on your rows and use .findChild(type(QtGui.QCheckBox())).isChecked() on the proper column

    such as :

    from PyQt4 import QtGui, QtCore
    from PyQt4.QtCore import Qt
    
    class Window(QtGui.QWidget):
        def __init__(self, rows, columns):
            QtGui.QWidget.__init__(self)
            self.table = QtGui.QTableWidget(rows, columns, self)
            for row in range(rows):
                qwidget = QtGui.QWidget()
                checkbox = QtGui.QCheckBox()
                checkbox.setCheckState(QtCore.Qt.Unchecked)
                qhboxlayout = QtGui.QHBoxLayout(qwidget)
                qhboxlayout.addWidget(checkbox)
                qhboxlayout.setAlignment(Qt.AlignCenter)
                qhboxlayout.setContentsMargins(0, 0, 0, 0)
                self.table.setCellWidget(row, 0, qwidget)
                self.table.setItem(row, 1, QtGui.QTableWidgetItem(str(row)))
            layout = QtGui.QVBoxLayout(self)
            self.button = QtGui.QPushButton()
            self.button.setObjectName("loadButton")
            layout.addWidget(self.table)
            layout.addWidget(self.button)
            self.button.clicked.connect(self.ButtonClicked)
    
        def ButtonClicked(self):
            checked_list = []
            for i in range(self.table.rowCount()):
                if self.table.cellWidget(i, 0).findChild(type(QtGui.QCheckBox())).isChecked():
                    checked_list.append(self.table.item(i, 1).text())
            print checked_list
    
    
    if __name__ == '__main__':
    
        import sys
        app = QtGui.QApplication(sys.argv)
        window = Window(3, 2)
        window.resize(350, 300)
        window.show()
        sys.exit(app.exec_())
    

提交回复
热议问题