PyQt:从QComboBox获取突出显示的索引

时间:2017-09-12 08:12:57

标签: python qt pyqt pyside

我想获取QComboBox中突出显示的项目的索引。如何访问此变量?

我的示例代码:

    ...
    self.combobox = QtWidgets.QComboBox()
    self.combobox.addItems(['a', 'b', 'c'])
    self.combobox.highlighted.connect(self.return_higlighted_index)
    ...

def return_highlighted_index(self):
    print('The current highlighted index is: ', '?')

2 个答案:

答案 0 :(得分:1)

而不是

self.combobox.activated[str].connect(self.return_higlighted_index)

def return_highlighted_index(self, combobox_entry):
    idx = self.combobox.findText(combobox_enty)
    print('The current highlighted index is: {}'.format(idx))

尝试这样的事情(未经测试)

return_highlighted_index

因此,您将方法return_highlighted_index连接到组合框,只要它被更改,它会将当前突出显示的字符串传递给方法combobox_entry,并return_highlighted_index() angular.element,您应该能够获得指数。

答案 1 :(得分:0)

我找到了解决方案,代码应该修改如下

    ...
    self.combobox = QtWidgets.QComboBox()
    self.combobox.addItems(['a', 'b', 'c'])
    self.combobox.highlighted[int].connect(self.return_higlighted_index)
    ...

def return_highlighted_index(self, param):
    print('The current highlighted index is: ', param)