How can I get the selected VALUE out of a QCombobox?

sabbour picture sabbour · Jan 13, 2010 · Viewed 143.2k times · Source

In Qt, I can get the selected text of a QComboBox by using the combobox->currentText() method. How can I get the selected value?

I searched for help but I couldn't find a method currentData() which I expected to find. I could only find combobox->currentIndex()

Is there a smarter way to do it other than combobox->itemData(combobox->currentIndex())?

Update: This is no longer necessary as of Qt 5. A currentData() method has been added http://doc.qt.io/qt-5/qcombobox.html#currentData-prop

Answer

Patrice Bernassola picture Patrice Bernassola · Jan 13, 2010

It seems you need to do combobox->itemData(combobox->currentIndex()) if you want to get the current data of the QComboBox.

If you are using your own class derived from QComboBox, you can add a currentData() function.