PyQt 对齐复选框并将其放在每一行

2022-01-12 00:00:00 python pyqt pyqt5 qtablewidget checkbox

问题描述

我正在尝试这个 使用复选框.遗憾的是,它是为 C++ 制作的,对 Python 代码的任何改编都会导致此错误:'QWidget' object is not callable我想做的是在每一行添加一个复选框,这是我的代码:

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 callable What I wanna to do is to add a check-box on each row, this is my code:

    pWidget = QWidget()
    pCheckbox = QCheckBox()
    pLayout = QVBoxLayout()
    pLayout.addWidget(pCheckbox)
    pLayout.setAlignment(Qt.AlignCenter)
    pLayout.setContentsMargins(0, 0 ,0, 0)
    pWidget.setLayout(pLayout)

    for char in accounts:
        for columnNumber in range(numberColumns):
            chkBoxItem = QTableWidgetItem()
            chkBoxItem.setFlags(Qt.ItemIsUserCheckable | Qt.ItemIsEnabled)
            chkBoxItem.setCheckState(Qt.Unchecked)
            self.mainAccountTable.insertRow(currentRowCount)
            self.mainAccountTable.setItem(currentRowCount, 0, pWidget(chkBoxItem))
            self.mainAccountTable.setItem(currentRowCount, 1, QTableWidgetItem(data[1]))

如果我把 pLayout = Layout() 说它不是在 Python 中实现的.那么,我怎样才能添加在中心对齐的复选框,而不是每行附近的文本区域?提前感谢您的任何提示.

If I put pLayout = Layout() is saying that it isn't implemented in Python. So, how I can I add that checkbox aligned in center without the text area near for each row? Thanks in advance for any tips.

稍后在循环内移动代码是有效的,但我无法控制检查:

Later moving the code inside the loop is working , but I can't control the checks:

for char in accounts:
        for columnNumber in range(numberColumns):
            pWidget = QWidget()
            pCheckbox = QCheckBox()
            pLayout = QVBoxLayout(pWidget)
            pLayout.addWidget(pCheckbox)
            pLayout.setAlignment(Qt.AlignCenter)
            pLayout.setContentsMargins(0, 0 ,0, 0)
            pWidget.setLayout(pLayout)
            #chkBoxItem = QTableWidgetItem()
            #chkBoxItem.setFlags(Qt.ItemIsUserCheckable | Qt.ItemIsEnabled)
            #chkBoxItem.setCheckState(Qt.Unchecked)
            self.mainAccountTable.insertRow(currentRowCount)
            self.mainAccountTable.setCellWidget(currentRowCount, 0, pWidget)

这是一个屏幕截图我怎么知道我检查了什么并建立了集合列表?

This is a screenshoot How can I know what I checked and build set list?


解决方案

这里是 ekhumoro 的示例被点击时被检查:

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

但您也可以迭代您的行并在正确的列上使用 .findChild(type(QtGui.QCheckBox())).isChecked()

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

如:

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

相关文章