PyQt4,从comboBox获取当前文本 [英] PyQt4, getting current text from comboBox

查看:333
本文介绍了PyQt4,从comboBox获取当前文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有comboBox的非常简单的GUI,其中包含4个项目。

这四个项目中的每一个都做不同的事情,需要链接到 QLineEdit 框可以启用/禁用 QLineEdit 框,还可以基于当前选择添加占位符文本。

I have a pretty simple GUI with a comboBox, with 4 items.
Each of these four items do separate things, and need to be linked to QLineEdit boxes in terms of enabling/disabling the QLineEdit boxes, as well as being able to add placeholder text based on the current selection.

代码:

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        self.comboBox = QtGui.QComboBox(self.centralwidget)
        self.comboBox.setGeometry(QtCore.QRect(10, 10, 201, 26))
        self.comboBox.setObjectName(_fromUtf8("comboBox"))
        self.comboBox.addItem(_fromUtf8(""))
        self.comboBox.addItem(_fromUtf8(""))
        self.comboBox.addItem(_fromUtf8(""))
        self.comboBox.addItem(_fromUtf8(""))

        if self.comboBox.currentText() == 'Item1':
            self.lineEdit_5.setDisabled(True)
            self.lineEdit_4.setText('0')  

    def retranslateUi(self, MainWindow):
        self.comboBox.setItemText(0, _translate("MainWindow", "Item1", None))
        self.comboBox.setItemText(1, _translate("MainWindow", "Item2", None))
        self.comboBox.setItemText(2, _translate("MainWindow", "Item3", None))
        self.comboBox.setItemText(3, _translate("MainWindow", "Item4", None))

self.lineEdits 当然是 QLineEdit ,即 self.lineEdit_5 = QtGui.QLineEdit()

我在这里做什么错了?

PS:这不是完整的代码,它经过了大大简化,因此易于阅读,如果您需要更多信息,请告诉我

PS: This is far from the full code, this is drastically simplified so its easy to read, let me know if you need more info

推荐答案

您需要使用信号和插槽

只要在 comboBox 中选择了新项目,信号 currentIndexChanged(const QString& text)发出( text 是所选新项目的文本)。您可以将一个方法连接到该信号,然后通过行编辑执行所需的任何操作。

Whenever a new item is selected in the comboBox, the signal currentIndexChanged(const QString & text) is emitted (text being the text of the new item selected). You can connect a method to this signal, and do whatever you need with the line edits.

    self.comboBox.currentIndexChanged[str].connect(self.onChange)

def onChange(self, newText):
    if newText=="Item 1":
        #do this
    else:
        #do that

这篇关于PyQt4,从comboBox获取当前文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆