选项与复选框的交互式交互

时间:2019-04-05 01:11:08

标签: python pyqt pyqt4 qmenu

我有一个弹出窗口,其中读取选项列表,并将其填充到groupbox和复选框中。

我有一个选择,其中包含以下数据: my_selection = {'drinks': ['coffee'], 'snacks': ['m&m']} 并且我正在尝试在弹出窗口中选中选项-coffee和-m&m

无论我的选择如何变化,这意味着itemA可能有drinks个项目,如果我选择itemB(itemA不再处于选择状态),则它可能有drinkssnacks中有2个项目,我想确保选中了正确的选项,以防是否有相同的项目命名,但类别不同,反之亦然。

我尝试将选择情况插入到类中,如下所示:

class FormWindow(QtGui.QWidget):
    def __init__(self, main_items, my_selection, parent=None, callback=None):
        ...
        if my_selection:
            for k, v in my.items():
                for i in v:
                    if sub_chk.text() == i:
                        sub_chk.setChecked(True)

它仅检查找到的最新项目,在这种情况下,仅考虑Snacks中的项目。

import sys
from PyQt4 import QtGui, QtCore
from collections import defaultdict


class FormWindow(QtGui.QWidget):
    def __init__(self, main_items, parent=None, callback=None):
        super(FormWindow, self).__init__(parent=parent)
        self.callback = callback

        layout = QtGui.QVBoxLayout()
        self.checkbox_options = []
        self.menu_tag_dict = defaultdict(set)

        for main_name, sub_name in main_items.items():
            # Set the main item
            groupbox = QtGui.QGroupBox(self)
            groupbox.setTitle(main_name.title())
            groupbox.setLayout(QtGui.QVBoxLayout())
            layout.addWidget(groupbox)

            if sub_name:
                # sub_txt = [(action.text()) for action in sub_name]
                sub_txt = [action for action in sub_name]

                # Creates QCheckbox for each option
                for s in sub_txt:
                    sub_chk = QtGui.QCheckBox(s)
                    self.checkbox_options.append(sub_chk)
                    groupbox.layout().addWidget(sub_chk)

        layout.addStretch()

        self.setLayout(layout)
        self.setWindowTitle('Form Checking')
        self.show()



if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    main_items = {'drinks': ['coffee', 'tea', 'water'], 'snacks': ['biscuits', 'm&m']}
    my_win = FormWindow(main_items)
    sys.exit(app.exec_())

# Example1 of what my selection will return
my_selection = {'drinks': ['coffee'], 'snacks': ['m&m']}

# Example2 of what my selection will return
my_selection = {'drinks': ['water', 'coffee'], 'snacks': ['biscuits']}

1 个答案:

答案 0 :(得分:2)

假设您要获取选中的项目,那么您需要做的是通过根据选中的状态删除或添加选择内容来反映数据,在下面的示例中,如果您按下按钮,选中的项目将被打印:

import sys
from PyQt4 import QtGui, QtCore
from functools import partial


class FormWindow(QtGui.QWidget):
    checkbox_options_changed = QtCore.pyqtSignal(dict)

    def __init__(self, main_items, parent=None, callback=None):
        super(FormWindow, self).__init__(parent=parent)
        self.callback = callback

        layout = QtGui.QVBoxLayout(self)
        self.checkbox_options = {}

        for main_name, sub_name in main_items.items():
            groupbox = QtGui.QGroupBox()
            groupbox.setTitle(main_name.title())
            lay = QtGui.QVBoxLayout(groupbox)
            layout.addWidget(groupbox)
            self.checkbox_options[main_name] = set()
            for s in sub_name:
                sub_chk = QtGui.QCheckBox(s)
                info = (main_name, s)
                wrapper = partial(self.on_stateChanged, info)
                sub_chk.stateChanged.connect(wrapper)
                lay.addWidget(sub_chk)
        layout.addStretch()
        self.setWindowTitle("Form Checking")

    @QtCore.pyqtSlot(tuple, QtCore.Qt.CheckState)
    def on_stateChanged(self, info, state):
        name, item = info
        option = self.checkbox_options[name]
        if state == QtCore.Qt.Checked:
            option.add(item)
        else:
            if item in option:
                option.remove(item)
        self.checkbox_options_changed.emit(self.checkbox_options)

    def get_checked_items(self):
        return self.checkbox_options


if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    main_items = {
        "drinks": ["coffee", "tea", "water"],
        "snacks": ["biscuits", "m&m"],
    }
    my_win = FormWindow(main_items)
    w = QtGui.QWidget()
    lay = QtGui.QVBoxLayout(w)
    button = QtGui.QPushButton("Print me")
    lay.addWidget(button)
    lay.addWidget(my_win)

    def on_clicked():
        print(my_win.get_checked_items())

    button.clicked.connect(on_clicked)
    my_win.checkbox_options_changed.connect(print)
    w.show()
    sys.exit(app.exec_())

enter image description here

{'drinks': {'coffee', 'tea'}, 'snacks': set()}

enter image description here

{'drinks': {'coffee', 'tea'}, 'snacks': {'biscuits'}}