QComboBox: how to get selected value?
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.
Code:
// 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:
Code:
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?
Re: QComboBox: how to get selected value?
You can use currentText() or you have the currentIndexChanged(...) signal (with int index or QString text parameter)
Re: QComboBox: how to get selected value?
I tried so:
Code:
void qpastebin::IndexChangedCode() {
codeList->setCurrentIndex(codeList->currentIndex());
}
void qpastebin::IndexChangedExpd() {
dataExp->setCurrentIndex(dataExp->currentIndex());
}
void qpastebin::IndexChangedPriv() {
apiPrivate->setCurrentIndex(apiPrivate->currentIndex());
}
and slots:
Code:
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
Re: QComboBox: how to get selected value?
Here you go, i just displayed the changing text in a QLabel:
Code:
#include <QApplication>
#include <QLabel>
#include <QComboBox>
#include <QVBoxLayout>
int main(int argc, char** argv) {
QLabel *label
= new QLabel("Here you will see the selected text from ComboBox",
&w
);
layout->addWidget(label);
layout->addWidget(combo);
combo->addItem("First text");
combo->addItem("Secont text");
combo->addItem("Third text");
w.show();
return a.exec();
}
Re: QComboBox: how to get selected value?
Thank you! That helped! :)