PyQT5 QComboBox - get value of combobox

前端 未结 1 961
萌比男神i
萌比男神i 2021-01-06 03:42

I am still very new to Qt but I am developing a type of calculator and want to use a combobox to select a coefficient. I have had success creating a combobox with a liststor

1条回答
  •  礼貌的吻别
    2021-01-06 04:30

    You can use addItem to add a name (text) with an associated value (data):

        self.combo.addItem('Foo', 23)
        self.combo.addItem('Bar', 42)
    

    A slot can be connected to the activated signal of the combo box, which will send the index of the item selected by the user:

        self.combo.activated.connect(self.handleActivated)
    

    You can then use itemText and itemData to access the name and value via the index parameter:

        def handleActivated(self, index):
            print(self.combo.itemText(index))
            print(self.combo.itemData(index))
    

    0 讨论(0)
提交回复
热议问题