PyQt allign checkbox and put it in every row

前端 未结 2 447
清歌不尽
清歌不尽 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_())
    
    0 讨论(0)
  • 2021-01-16 12:15

    You could consider using the Qt Designer, so that you can:

    • Get your desired layout visually with immediate feedback
    • Get to see the actual code generated from it and spot what you were missing

    After you get your desired window (i.e. the file with a .ui extension), you can use the pyuic5[1] utility, which will generate a Python file from the UI file. From the man page

    pyuic5 - compile Qt5 user interfaces to Python code

    Steps

    Simple steps with example

    Create and Save the UI

    You should use the Qt Designer and save the .ui file.

    Generate Python code from the UI file

    If your .ui file is named mainwindow.ui then you can use the command:

    pyuic5 mainwindow.ui -o mainwindow.py
    

    Update your Python code

    Get your Python code to use the generated Python-based UI file.

    from PyQt5.QtWidgets import QMainWindow
    
    from mainwindow import Ui_MainWindow   # <<--- important
    
    # Set up the user interface from Designer.
    win = QMainWindow()
    gui = Ui_MainWindow()
    gui.setupUi(win)
    

    As you can see above, you need to import the class representing the UI from the Python module that was generated, in our case being the mainwindow.py file from the command in the prev. step.

    The class is automatically prefixed with Ui_ by the utility. You then instantiate a QMainWindow and the generated class and use Qt's built-in method setupUi for it to incorporate all the widgets, etc.

    Important: Every time you update your window in Qt Designer, you'll need to repeat step #2. Considering the amount of time saved by using the Designer directly, this shouldn't be a problem.

    Note: You can use the generated mainwindow.py file to read the code and see how the desired layout was achieved. This should be useful if you really do not want to continue using this approach.

    [1] The pyuic5 command is found within the pyqt5-dev-tools package, so a sudo apt-get install pyqt5-dev-tools should do it.

    0 讨论(0)
提交回复
热议问题