user-2147482637
user-2147482637

Reputation: 2163

How to get current value from PyQt comboBox

I am using PyQt designer and then converting it using pyuic4.
My ui file has this comboBox that looks like this:

    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.

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. I tried doing some items on this http://pyqt.sourceforge.net/Docs/PyQt4/qcombobox.html but i cant get it to work.

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:

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.

so, is this possible?

thanks

Upvotes: 0

Views: 7116

Answers (2)

PersianGulf
PersianGulf

Reputation: 2885

If you want to call a SLOT in connect function with argument passing you should using lambda such as :

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

Upvotes: 0

Scylardor
Scylardor

Reputation: 487

Well, about your errors, aren't they pretty self-explanatory ? :-)

From the documentation you just linked :

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().

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.

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.

See also setItemData().

The role parameter has a default value so you can omit it, but you have to give the index.

Glad you found the solution anyway !

Upvotes: 2

Related Questions