How can I get the selected VALUE out of a QCombobox?
Solution 1:
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.
Solution 2:
This one can get the text of current index:
QString cb = cbChoice ->currentText();
Solution 3:
you can set QVariant data for all items, then you can get the value when you need it.
there is an example code for this situation:
ui.comboBoxSheetSize->addItem("128 m", QVariant(128));
ui.comboBoxSheetSize->addItem("256 m", QVariant(256));
ui.comboBoxSheetSize->addItem("512 m", QVariant(512));
ui.comboBoxSheetSize->addItem("1024 m", QVariant(1024));
...
void Page::onComboSheetSizeChanged( int index )
{
int value = ui.comboBoxSheetSize->itemData(index).toInt();
}
by the way, i think i misunderstood your question. i think the way you get data is smart enough?
Solution 4:
The member function QComboBox::currentData has been added since this question was asked, see this commit