user3030327
user3030327

Reputation: 451

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

I have two combo boxes and 3 lists as shown in the below.

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

I added list1 into first Combo Box with addItems(list1) The question is that i want to show, if i select 'one' in the first Qcombobox that the second Qcombo box should show 'list2', and if i select 'two' that should show 'list3'. Please Let me know how to do.

Thank you.

Upvotes: 1

Views: 308

Answers (1)

user3030327
user3030327

Reputation: 451

I used two Methods to solve,

  1. currentIndexChanged
  2. currentIndex()

Called Method as bellow:


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)

Alternatively to selecting the indexes, you can use self.posten.currentText() == "one":

Upvotes: 1

Related Questions