如何从 QCombobox 中获取选定的值?

在 Qt 中,我可以通过使用 combobox->currentText()法。 我怎样才能得到 selected value

我寻求帮助,但我找不到一个方法 currentData(),我希望找到。我只能找到 combobox->currentIndex()

除了 combobox->itemData(combobox->currentIndex())还有更聪明的方法吗?

更新: 从 Qt 5开始,这就不再需要了。 currentData()方法已经被加入 http://doc.qt.io/qt-5/qcombobox.html#currentData-prop

174480 次浏览

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?

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.

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();
}

This one can get the text of current index:

QString cb = cbChoice ->currentText();

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

I had the issue and

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

solved this.

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

I had same issue

I have solved by

value = self.comboBox.currentText()
print value

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)));
}

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 !

if you are developing QGIS plugins then simply

self.dlg.cbo_load_net.currentIndex()

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

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