python - 如何访问 PySide2 中选中的 QCheckBox?

标签 python python-3.x pyside2

我正在 PySide2 中开发一个 GUI,其中我使用循环在 QScrollArea 中显示 QCheckBoxes,并且我想访问用户选中的那些复选框。

import sys
from PySide2 import QtWidgets, QtCore
from PySide2.QtWidgets import QCheckBox
class MyWindow(QtWidgets.QMainWindow):
    def __init__(self, data):
        super().__init__()

        self.data = data

        self.scrollArea = QtWidgets.QScrollArea()
        self.scrollArea.setWidgetResizable(True)
        self.setCentralWidget(self.scrollArea)
        self.widget = QtWidgets.QWidget()
        self.scrollArea.setWidget(self.widget)

        button = QtWidgets.QPushButton("Click me")
        button.clicked.connect(self.onButton)

        self.grid = QtWidgets.QGridLayout(self.widget)
        self.grid.addWidget(button)

    def onButton(self):
        row = 1
        for item in self.data:
            checkBox_measurement = QCheckBox(item)
            self.grid.addWidget(checkBox_measurement, row, 0)

            line = QtWidgets.QFrame()
            line.setFrameShape(QtWidgets.QFrame.HLine)
            self.grid.addWidget(line, row+1, 0, 1, 2)
            row += 2
data = ['Title 1', 'Title 2', 'Title 3' , 'Title 4', 'Title 5', 'Title 6' ,'Title 7', 'Title 8', 'Title 9' ]
if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    myWindow = MyWindow(data)
    myWindow.resize(500, 300)
    myWindow.show()
    app.exec_()

这是在我的窗口中创建复选框的代码,我如何知道用户选中了哪些复选框

最佳答案

尝试一下:

import sys
#from PySide2 import QtWidgets, QtCore
#from PySide2.QtWidgets import QCheckBox
from PyQt5 import QtWidgets, QtCore
from PyQt5.QtWidgets import QCheckBox

class MyWindow(QtWidgets.QMainWindow):
    def __init__(self, data):
        super().__init__()

        self.data = data

        self.scrollArea = QtWidgets.QScrollArea()
        self.scrollArea.setWidgetResizable(True)
        self.setCentralWidget(self.scrollArea)
        self.widget = QtWidgets.QWidget()
        self.scrollArea.setWidget(self.widget)

        button = QtWidgets.QPushButton("Click me")
        button.clicked.connect(self.onButton)

        self.grid = QtWidgets.QGridLayout(self.widget)
        self.grid.addWidget(button)

    def onButton(self):
        row = 1
        for item in self.data:
            checkBox_measurement = QCheckBox(item)
            checkBox_measurement.stateChanged.connect(self.clickBox)     # +

            self.grid.addWidget(checkBox_measurement, row, 0)

            line = QtWidgets.QFrame()
            line.setFrameShape(QtWidgets.QFrame.HLine)
            self.grid.addWidget(line, row+1, 0, 1, 2)
            row += 2


    def clickBox(self, state):                                           # +
        print(state, self.sender().text())                               # +


data = ['Title 1', 'Title 2', 'Title 3' , 'Title 4', 'Title 5', 'Title 6' ,'Title 7', 'Title 8', 'Title 9' ]

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    myWindow = MyWindow(data)
    myWindow.resize(500, 300)
    myWindow.show()
    app.exec_()

enter image description here

关于python - 如何访问 PySide2 中选中的 QCheckBox?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58345359/

相关文章:

python - Pandas - 使用 read_csv 读取时间

python - 使用 Python 进行 Windows 进程管理

python - 向我的船指向的方向发射子弹

python - QTreeWidgetItem.addChild() 在某些情况下不起作用?

python - 如何修复 QFocusEvent 行为

python - 如何将 X 列的所有条目乘以常数,其中 X 是变量

python - 在 Flask-UWSGI 应用程序中处理耗时的请求

python - 是否可以动态地在 python 对象中生成属性?

python-3.x - 搜索特定字符之间的最后一组文本

python - QRectF 没有出现在我的 QGraphicsScene 中