QComboBox::currentText()
-
my problem still non solved:
i start a program and change item manually (from 1 to 2), so in my opinion currentText() is "2" now, but QLabel always show me "abc1", why? i change it!
@
{
QApplication a(argc, argv);
QWidget w;
QComboBox* cmb = new QComboBox;
QLabel* lbl = new QLabel();
cmb->addItem("1");
cmb->addItem("2");
lbl->setText("abc"+cmb->currentText());
cmb->show();
lbl->show();return a.exec();
}
@ -
"QLabel" is not aware that you changed selection.
use QComboBox::currentIndexChanged signal to update your QLabel.
-
how should the label know that it has to display another text?
you have to connect QComboBox::currentTextChanged to QLabel::setText (or your own slot, if you want to modify the text before displaying it)
-
You have a fundamental problem with the setup of your application. You are showing "main.cpp". This part will be executed and the program will "wait" in a.exec() when you are able to change the items in the combobox. As mentioned by the others above you require the signal & slot logic for proper working. You should study some of the examples eg. "this one":http://qt-project.org/doc/qt-5.0/qtwidgets/dialogs-standarddialogs.html
-
ok. how about that?
@
QObject::connect(cmb, SIGNAL(currentTextChanged(QString)),
cmb, SLOT (setCurrentText(QString))
); // now combobox know about changed item yes? or its just stupid lines?if (cmb->currentText() == "Item2") { //do something }
@
-
And what do you get after execution ? :)
-
i dont know :)
i just need to change my cmb->currentText() after changing it manually (item 1 to item 2)
help me) -
@ QObject::connect(cmb, SIGNAL(currentIndexChanged(QString)),
lbl, SLOT (setText(QString)));@Try with this...
-
y i did it after 1-2 reply. it works but cmb->currentText() is stll have 1st item...
now i try to understand 3rd reply -
calling currentText() after the connect will of course give you the text of the first item, because it is executed before you change the selection in the GUI.