【问题标题】:Replace checkboxes accordingly to option chosen in QCombobox根据 QCombobox 中选择的选项替换复选框
【发布时间】:2019-06-26 12:43:39
【问题描述】:

我正在尝试自动填充排他复选框的列表,其中复选框的数量根据 QComboBox 中选择的选项而变化。

我被困在如何删除布局中最初附加的小部件并用新的小部件替换它们的部分。

例如,如果选择菜单项 -food,则会显示 3 个复选框('good2eat'、'macs'、'popeyes'),如果选择了drinks,则这 3 个复选框将被删除,并且替换为“水”、“茶”选项

class MenuWindow(QtGui.QWidget):
    def __init__(self, dict_items, parent=None):
        super(MenuWindow, self).__init__(parent=parent)

        layout = QtGui.QVBoxLayout()
        self.checkbox_options = {}
        self.menu_tag_dict = defaultdict(set)

        self.menu_combos = QtGui.QComboBox()
        self.menu_combos.currentIndexChanged.connect(self.get_selections)
        self.chkbox_group = QtGui.QButtonGroup()

        for menu_name, submenu_name in dict_items.items():
            self.menu_combos.addItems([menu_name])

            if submenu_name:
                sub_txt = [m for m in submenu_name]
                for s in sub_txt:
                    sub_chk = QtGui.QCheckBox(s)
                    self.checkbox_options[menu_name] = sub_chk
                    self.chkbox_group.addButton(sub_chk)

        print_btn = QtGui.QPushButton('Print selected')

        layout.addWidget(self.menu_combos)
        for s in self.checkbox_options.values():
            layout.addWidget(s)

        layout.addWidget(get_sel_btn)
        layout.addStretch()

        self.setLayout(layout)
        self.show()

    def get_selections(self):
        # Get combobox text
        combo_text = self.menu_combos.currentText()
        # get the menus
        items = self.menu_combos.get(combo_text)



my_items = {
    'food' : ['good2eat', 'macs', 'popeyes'],
    'drinks': ['water', 'tea']
}

myWin = MenuWindow(my_items)
myWin.show()

即便如此,在代码开头,菜单项food下填充的选项数量已经是错误的了。

有没有更好的方法来处理这个问题??

【问题讨论】:

    标签: python pyqt qcombobox qcheckbox


    【解决方案1】:

    试试看:

    import sys
    from PyQt5.QtGui     import *
    from PyQt5.QtWidgets import *
    from PyQt5.QtCore    import *
    
    class MenuWindow(QWidget):
        def __init__(self, dict_items, parent=None):
            super(MenuWindow, self).__init__(parent=parent)
    
            self.dict_items = dict_items
    
            self.menu_combos = QComboBox()
            self.menu_combos.currentIndexChanged[str].connect(self.get_selections)
            self.menu_combos.addItems(sorted(self.dict_items.keys()))
    
            print_btn = QPushButton('Print selected')        # ! Print selected
            print_btn.clicked.connect(self.printSelected)    # ! Print selected
    
            self.layoutV = QVBoxLayout()
            self.layoutV.addWidget(self.menu_combos)
    
            for s in self.dict_items[self.menu_combos.currentText()]:
                self.sub_chk = QCheckBox(s)
                self.layoutV.addWidget(self.sub_chk)    
    
            self.layoutV.addStretch()
            self.layoutV.addWidget(print_btn)                # ! Print selected
    
            self.setLayout(self.layoutV)
            self.show()
    
        @pyqtSlot(str)
        def get_selections(self, text):
    
            if self.layout():
                countLayout = self.layout().count()
                for it in range(countLayout - 3):             # <--- removeWidget
                    w = self.layout().itemAt(1).widget()
                    self.layout().removeWidget(w)     
                    w.hide()
    
                for s in self.dict_items[text][::-1]:         # <--- insertWidge
                    self.sub_chk = QCheckBox(s)
                    self.layoutV.insertWidget(1, self.sub_chk) 
    
        def printSelected(self):                              # ! Print selected
            checked_list = []
            for i in range(1, self.layoutV.count() - 2):
                chBox = self.layoutV.itemAt(i).widget()
                if chBox.isChecked():
                    checked_list.append(chBox.text())
            print("selected QCheckBox: " + str(list(checked_list)))  
    
    
    my_items = {
        'food'  : ['good2eat', 'macs', 'popeyes'],
        'drinks': ['water',    'tea']
    }
    
    if __name__ == '__main__':
        import sys
        app  = QApplication(sys.argv)
        myWin = MenuWindow(my_items)
        myWin.setGeometry(300, 150, 250, 250)
        myWin.show()
        sys.exit(app.exec_())
    

    【讨论】:

      猜你喜欢
      • 2012-07-03
      • 1970-01-01
      • 2011-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多