Combobox
-
i have combobox(cmb) (2 items)
i have button (btn)
i have widget (dial) (hidden)@
if (cmb->itemClicked("item2") ) {
QObject::connect(btn, SIGNAL(clicked()),
dial, SLOT (show())
);
}
@
i dont know how make this condition...
just correct my 1st line
(show widget if 1. item2 selected 2. button clicked) -
With "QComboBox":http://qt-project.org/doc/qt-5.0/qtwidgets/qcombobox.html you can use the "currentIndex":http://qt-project.org/doc/qt-5.0/qtwidgets/qcombobox.html#currentIndex-prop or the "currentText property":http://qt-project.org/doc/qt-5.0/qtwidgets/qcombobox.html#currentText-prop. If you want an easy signal-slot connection, though, there are currentIndexChanged(int index) and currentTextChanged(const QString & text) signals.
With "QButton":http://qt-project.org/doc/qt-5.0/qtwidgets/qpushbutton.html you can use the "KeyPressEvent":http://qt-project.org/doc/qt-5.0/qtwidgets/qpushbutton.html#keyPressEvent - reimplement it from the base class; using the "clicked() signal":http://qt-project.org/doc/qt-5.0/qtwidgets/qabstractbutton.html#clicked seems also a good idea though.
Knowing that and the show() slot for your dial widget, what is there to not know? You just need to connect the signals to the slots.
-
you could also check the current index of the combobox when pushing the button
@void MainWindow::on_pushButton_clicked()
{
if (ui->cmb->currentIndex() == 1) { //second item
ui->dial->show();
}
}@if you don't want to use the index use currentText() instead
@void MainWindow::on_pushButton_clicked()
{
if (ui->cmb->currentText() == "item2") {
ui->dial->show();
}
}@ -
ty. its worked.
but now i have another problem@
int main(int argc, char argv[])
{
QApplication a(argc, argv);
QWidget w;
QComboBox cmb = new QComboBox;
//some lanother lines...
cmb->addItem("1");
cmb->addItem("2");
cmb->addItem("3");if (cmb->currentText() == "1") {
QObject::connect(btn, SIGNAL(clicked()),
dial, SLOT (show())
);
}
if (cmb->currentText() == "2") {
QObject::connect(btn, SIGNAL(clicked()),
time, SLOT (show())
);
}
@after starting program i can choose some items (1,2,3) but seems like its always "1" (2nd signal/slot worked if currentText is "1" , not "2")
Why can i see "2" , but Qt can see only "1"? -
I think the problem here is that you never disconnect the signal/slot functions, so Qt only executes the first one it registered. You would have to disconnect every time you want to connect the button anew.
Take a look at http://qt-project.org/doc/qt-4.8/qobject.html#disconnect .
While this seems possible, it certainly does not feel right. I would still recommend to check the combobox index/text when pushing the button. -
no its some comboBox's neglect:
@
{
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();
}
@
its always "abc1" -
You use a "QComboBox::addItem()":http://qt-project.org/doc/qt-5.0/qtwidgets/qcombobox.html#addItem function:
bq. Adds an item to the combobox with the given text, and containing the specified userData (stored in the Qt::UserRole). The item is appended to the list of existing items.
And then a "QComboBox::currentText()":http://qt-project.org/doc/qt-5.0/qtwidgets/qcombobox.html#currentText-prop function:
bq. This property holds the current text.
If the combo box is editable, the current text is the value displayed by the line edit. Otherwise, it is the value of the current item or an empty string if the combo box is empty or no current item is set.But nowhere do you change the current QComboBox item - no wonder you always get the first item you inserted into QComboBox. You need to use "QComboBox::setCurrentText()":http://qt-project.org/doc/qt-5.0/qtwidgets/qcombobox.html#currentText-prop or "QComboBox::setCurrentIndex()":http://qt-project.org/doc/qt-5.0/qtwidgets/qcombobox.html#currentIndex-prop if you want another QComboBox entry to be the current one.