简体   繁体   English

如何从PyQt comboBox获取当前值

[英]How to get current value from PyQt comboBox

I am using PyQt designer and then converting it using pyuic4. 我正在使用PyQt设计器,然后使用pyuic4进行转换。
My ui file has this comboBox that looks like this: 我的ui文件有这个看起来像这样的组合框:

    self.comboBox = QtGui.QComboBox(self.groupBox_3)
    self.comboBox.setGeometry(QtCore.QRect(20, 30, 81, 22))
    self.comboBox.setObjectName(_fromUtf8("comboBox"))
    self.comboBox.addItem(_fromUtf8(""))
    self.comboBox.addItem(_fromUtf8(""))
    self.comboBox.addItem(_fromUtf8(""))
    self.comboBox.addItem(_fromUtf8(""))
    self.comboBox.addItem(_fromUtf8(""))
    self.comboBox.addItem(_fromUtf8(""))
    self.comboBox.addItem(_fromUtf8(""))
    self.comboBox.addItem(_fromUtf8(""))
    self.comboBox.addItem(_fromUtf8(""))
    self.comboBox.addItem(_fromUtf8(""))
    self.comboBox.addItem(_fromUtf8(""))
    self.comboBox.addItem(_fromUtf8(""))

so as you can see, the text that i put in my combo box doesnt show up in this part of the code, however it does show up in the gui. 所以你可以看到,我放在我的组合框中的文本没有显示在代码的这一部分中,但是它确实显示在gui中。

usually when I have some user selection, like a radio button, I can do this check: 通常当我有一些用户选择,如单选按钮,我可以做这个检查:

if self.RAnkle.isChecked():

i can do similar things with input text. 我可以用输入文本做类似的事情。 I am trying to do this logic with the comboBox, like I want to say self.comboBox.getText() and return the string that the user has selected. 我试图用comboBox做这个逻辑,就像我想说self.comboBox.getText()并返回用户选择的字符串。 I tried doing some items on this http://pyqt.sourceforge.net/Docs/PyQt4/qcombobox.html but i cant get it to work. 我尝试在这个http://pyqt.sourceforge.net/Docs/PyQt4/qcombobox.html做一些项目,但我不能让它工作。

For example, this code: 例如,这段代码:

self.comboBox.activated()  

Returns this error message: 返回此错误消息:

TypeError: native Qt signal is not callable

I have also tried to use the itemData() but i still receive an error: 我也试过使用itemData()但我仍然收到一个错误:

TypeError: QComboBox.itemData(int, int role=Qt.UserRole): not enough arguments

I am doing this inside of the callback, so i first do this: 我在回调中这样做,所以我首先这样做:

self.analyzeButton.clicked.connect(self._AnalyzeData)

Then inside the function _AnalyzeData I am trying to get the text of the current combobox item. 然后在函数_AnalyzeData内部我试图获取当前组合框项目的文本。

so, is this possible? 那么,这可能吗?

thanks 谢谢

Well, about your errors, aren't they pretty self-explanatory ? 那么,关于你的错误,他们不是很明显吗? :-) :-)

From the documentation you just linked : 从您刚刚链接的文档:

void activated (int) void activated(int)

This is the default overload of this signal.This signal is sent when the user chooses an item in the combobox. 这是此信号的默认过载。当用户选择组合框中的项目时,将发送此信号。 The item's index is passed. 项目的索引已通过。 Note that this signal is sent even when the choice is not changed. 请注意,即使未更改选项,也会发送此信号。 If you need to know when the choice actually changes, use signal currentIndexChanged(). 如果您需要知道选择何时实际更改,请使用信号currentIndexChanged()。

As a Qt signal it cannot directly be called: it can be emitted, and you can create a slot that will be called when it happens. 作为Qt信号,它不能直接被调用:它可以被发射,你可以创建一个在它发生时被调用的槽。

QVariant QComboBox.itemData (self, int index, int role = Qt.UserRole) QVariant QComboBox.itemData(self,int index,int role = Qt.UserRole)

Returns the data for the given role in the given index in the combobox, or QVariant.Invalid if there is no data for this role. 返回组合框中给定索引中给定角色的数据,如果没有此角色的数据,则返回QVariant.Invalid。

See also setItemData(). 另请参见setItemData()。

The role parameter has a default value so you can omit it, but you have to give the index. role参数有一个默认值,因此您可以省略它,但您必须提供索引。

Glad you found the solution anyway ! 很高兴你找到了解决方案!

If you want to call a SLOT in connect function with argument passing you should using lambda such as : 如果你想在带有参数传递的connect function调用SLOT ,你应该使用lambda,例如:

QtCore.QObject.connect(self.pushButton_2, QtCore.SIGNAL(_fromUtf8("clicked()")), 
                               lambda: self.materialsInstance.setFilterDict_Insert("L",self,"name",self.lineEdit.text()))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM