如何基于QcomboBox索引切换小部件的启用状态

时间:2013-01-26 20:52:02

标签: python pyqt pyqt4

我正在使用pyQT 4.8.3为QGIS插件创建一个合适的GUI

表单中有三个小部件

my_comboBox , my_lineEdit , my_spinBox

假设comboBox有三个条目

'combo_first_item' , 'combo_second_item' , 'combo_third_item'

我到底想要的是什么;

if 'combo_second_item' is selected, then my_lineEdit toggles state to disabled
if 'combo_third_item' selected, then my_spinBox toggles state to disabled

那么,如何根据组合框中选定的字符串(或索引值)切换窗体中窗口小部件的启用状态?

什么是正确的信号 - >插槽分配? 与QbuttonBox不同,QcomboBox不会触发SetDisabled插槽

感谢。

1 个答案:

答案 0 :(得分:2)

创建一个将字符串映射到widget的字典:

widgets = {'combo_first_item': my_comboBox,
           'combo_second_item': my_lineEdit,
           'combo_third_item': my_spinBox}

还有一个插槽:

def disableWidget(currentIndex):
     widget = widgets[currentIndex]
     widget.setEnabled(False)
     # or anything else you want to do on the widget

然后您可以将currentIndexChanged[QString]信号连接到此:

comboBox.currentIndexChanged['QString'].connect(disableWidget)

或者,您可以使用currentIndexChanged[int]和列表而不是字典。

PS:如果这是在一个类实例中,请相应地放置self