如何在 QcomboBox 中显示项目取决于第二个 QcomboBox 选择的项目

How to show items in QcomboBox depends on second QcomboBox selected item

我有两个组合框和 3 个列表,如下所示。

list1 = ['one','two'] list2 = ['a','b'] list3 = ['1','2']

我使用 addItems(list1) 将 list1 添加到第一个组合框中 问题是我想显示,如果我 select 'one' 在第一个 Qcombobox 中第二个 Qcombo 框应该显示 'list2',如果我 select 'two' 应该显示 'list3'。请让我知道该怎么做。

谢谢。

我用了两种方法解决,

  1. currentIndexChanged
  2. currentIndex()

调用方法如下:


list1 = ['one','two']
comboBox_1.addItems(list1)
    
comboBox_1.currentIndexChanged.connect(self.item_name)

def item_name(self):
    list2 = ['a','b'] 
    list3 = ['1','2']
    
    if comboBox_1.currentIndex() == 0:
        comboBox_2.addItems(list2)
    
    elif comboBox_1.currentIndex() == 1:
        comboBox_2.addItems(list3)

除了选择索引,您还可以使用 self.posten.currentText() == "one":