PDA

View Full Version : QComboBox: how to get selected value?



Ivan Khromov
2nd April 2011, 11:22
Good day! I'm newbie in Qt, please, help me!
When I was writing my programs on C# 2008, there have been many ways to get the current value of the ComboBox. In Qt, I create QComboBox and insert items without using UI designer.

dataExp = new QComboBox();
codeList = new QComboBox();
apiPrivate = new QComboBox();
// items: codeList
codeList->addItem("PHP", "php");
codeList->addItem("JavaScript", "javascript");
// end of items for codeList
//items: dataExp
dataExp->addItem("Never delete", "N");
dataExp->addItem("10 Minutes", "10M");
dataExp->addItem("1 Hour", "1H");
dataExp->addItem("1 Day", "1D");
dataExp->addItem("1 Month", "1M");
// end of items for dataExp
// items: apiPrivate
apiPrivate->addItem("Public", "0");
apiPrivate->addItem("Private", "1");
// end of items for apiPrivate

My question: now to get value from selected item?
I try that:


private_stat = apiPrivate->itemText(apiPrivate->currentIndex());
prog_lang = codeList->itemText(codeList->currentIndex());
date_expire = dataExp->itemText(dataExp->currentIndex());

But returned default value :(
Tell me, what to do?

Zlatomir
2nd April 2011, 11:31
You can use currentText() (http://doc.qt.nokia.com/latest/qcombobox.html#currentText-prop) or you have the currentIndexChanged(...) signal (http://doc.qt.nokia.com/latest/qcombobox.html#currentIndexChanged) (with int index or QString text parameter)

Ivan Khromov
2nd April 2011, 12:01
I tried so:

void qpastebin::IndexChangedCode() {
codeList->setCurrentIndex(codeList->currentIndex());
}
void qpastebin::IndexChangedExpd() {
dataExp->setCurrentIndex(dataExp->currentIndex());
}
void qpastebin::IndexChangedPriv() {
apiPrivate->setCurrentIndex(apiPrivate->currentIndex());
}
and slots:

private_stat = apiPrivate->itemData(apiPrivate->currentIndex()).toString();
prog_lang = codeList->itemData(codeList->currentIndex()).toString();
date_expire = dataExp->itemData(dataExp->currentIndex()).toString();
connect(codeList, SIGNAL(currentIndexChanged(int)), this, SLOT(IndexChangedCode()));
connect(dataExp, SIGNAL(currentIndexChanged(int)), this, SLOT(IndexChangedExpd()));
connect(apiPrivate, SIGNAL(currentIndexChanged(int)), this, SLOT(IndexChangedPriv()));
But all the same data from QComboBox only those who are selected by default ...
Please show an example would be very grateful

Zlatomir
2nd April 2011, 12:14
Here you go, i just displayed the changing text in a QLabel:

#include <QApplication>
#include <QLabel>
#include <QComboBox>
#include <QVBoxLayout>

int main(int argc, char** argv) {

QApplication a(argc, argv);
QWidget w;

QVBoxLayout *layout = new QVBoxLayout(&w);
QLabel *label = new QLabel("Here you will see the selected text from ComboBox", &w);
QComboBox *combo = new QComboBox(&w);
layout->addWidget(label);
layout->addWidget(combo);
combo->addItem("First text");
combo->addItem("Secont text");
combo->addItem("Third text");

QObject::connect(combo, SIGNAL(currentIndexChanged(QString)), label, SLOT(setText(QString)));

w.show();

return a.exec();
}

Ivan Khromov
2nd April 2011, 13:05
Thank you! That helped! :)