87

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

sabbour
  • 4,730
  • 4
  • 32
  • 33

13 Answers13

90

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.

Patrice Bernassola
  • 13,271
  • 3
  • 41
  • 54
  • 2
    Thanks. I just used this tip in my program. – Brian Stinar Oct 08 '10 at 20:06
  • 1
    @Patrice Bernassola However the switch operation of type 'QVariant' :`combobox->itemData(combobox->currentIndex())` is illegal !!! Why ? – The Beast Jan 23 '16 at 01:49
  • This doesn't work if nothing is selected i.e. `currentIndex = -1`. It will either return the data from the last index or raise an error if the QComboBox is empty. – johnson Jan 09 '18 at 15:10
25

This one can get the text of current index:

QString cb = cbChoice ->currentText();
littlecodefarmer758
  • 914
  • 2
  • 10
  • 22
23

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?

ufukgun
  • 6,459
  • 7
  • 29
  • 54
11

The member function QComboBox::currentData has been added since this question was asked, see this commit

joaerl
  • 822
  • 1
  • 8
  • 16
  • For documentation see: http://doc.qt.io/qt-5/qcombobox.html#currentData-prop. The property is available since Qt 5.2. – m4tx Jan 03 '16 at 17:17
7

I had same issue

I have solved by

value = self.comboBox.currentText()
print value
jatinkumar patel
  • 2,574
  • 18
  • 27
3

This is my OK code in QT 4.7:

 //add combobox list 
    QString val;
   ui->startPage->clear();
    val = "http://www.work4blue.com";
    ui->startPage->addItem(tr("Navigation page"),QVariant::fromValue(val));
    val = "https://www.google.com";
    ui->startPage->addItem("www.google.com",QVariant::fromValue(val));
    val = "www.twitter.com";
    ui->startPage->addItem("www.twitter.com",QVariant::fromValue(val));
    val = "https://www.youtube.com";
    ui->startPage->addItem("www.youtube.com",QVariant::fromValue(val));

   // get current value
    qDebug() << "current value"<< 
       ui->startPage->itemData(ui->startPage->currentIndex()).toString();
bluedrum
  • 86
  • 1
  • 4
1

I'm astonished that there isn't an activated signal and have the same problem. I solved it by making a subclass of QComboBox. I think it's better to avoid having to directly access the object and call its functions because that means more tight coupling and goes against Qt's philosophy. So here's the class I made that works for me.

class SmartComboBox : public QComboBox {

    Q_OBJECT

private slots:

    void triggerVariantActivated(int index);

public:

    SmartComboBox(QWidget *parent);

signals:

    void activated(const QVariant &);

};

And the implementation

void SmartComboBox::triggerVariantActivated(int index)
{
    activated(itemData(index));
}

SmartComboBox::SmartComboBox(QWidget *parent)
:QComboBox(parent)
{
    connect(this, SIGNAL(activated(int)), this, SLOT(triggerVariantActivated(int)));
}
pinkboi
  • 259
  • 2
  • 9
0

I did this

QDir path("/home/user/");
QStringList _dirs = path.entryList(QDir::Dirs);
std::cout << "_dirs_count = " << _dirs.count() << std::endl;
ui->cmbbox->addItem(Files);
ui->cmbbox->show();

You will see with this that the QStringList named _dirs is structured like an array whose members you can access via an index up to the value returned by _dirs.count()

E Purdy
  • 131
  • 1
  • 8
0

The question is old, but maybe, somebody need an actual answer.

In the QGIS 3.4 you can get the value from the QComboBox with the method currentData().

Example: comboBox.currentData()

Link: https://doc.qt.io/qt-5/qcombobox.html#currentData-prop

Nils
  • 818
  • 7
  • 23
deit
  • 9
  • 1
  • 2
-1

I had the issue and

QString str = m_UI->myComboBox->currentText();

solved this.

kiriloff
  • 22,522
  • 32
  • 127
  • 207
-1

if you are developing QGIS plugins then simply

self.dlg.cbo_load_net.currentIndex()
Abhijit Gujar
  • 393
  • 3
  • 15
  • Nope, this is as well not *selected `VALUE`*. Selected *`TEXT`*, selected *`VALUE`* and selected *`INDEX`* are completely different things. Only in a coincidence may `index` be equal to `value`. If people want to use combo, they need to learn and understand the difference. And to my best understanding, in Qt the VALUE is a bit extended when using data models. – Oak_3260548 Dec 30 '20 at 08:13
-2

I know I'm very late but for those who still have that problem, it can be solved easily. I use Qt 5.3 and it works fine. No need to create a function or all that.

int valueComboBox;
valueComboBox = comboBox->currentIndex();

and it works ! Hope it helps !

-5

I confirm the easiest way is to do this:

uiAnalyseAssets::AnalyseAssets(QWidget *parent)
: QWidget(parent)
{
ui.comboBox->addItem("text1");
ui.comboBox->addItem("text2");

...
}

void mainFunction::yourFunction( int index )
{
 int value = ui.comboBox->currentText();
}
Brad Mace
  • 26,280
  • 15
  • 94
  • 141
Tarik
  • 1
  • 1
  • 7
    Wait a second -- doesn't `QComboBox::currentText()` return a `QString`? How do you arrive at a sensible `int`? And what do you want to do with your parameter `int index`? – Christian Severin May 03 '11 at 12:07