'If' does not work
I'm trying to do a IF-Query in my QT Creator 5.0.2 (and the Designer). Here's my code for mainwindow.cpp:
MainWindow::MainWindow(QWidget *parent) :
if (ui->comboBox->currentText() == "Digital IO") ui->pushButton->setVisible(false); else ui->pushButton->setVisible(true);
When I change the combobox item, nothing happens with the button. Help?
Please wrap your code in '@' tags and format it properly.
The code above is placed in the constructor - the if statement runs only once, when MainWindow is created. To get updates whenever comboBox state changes, connect one of it's signals to a custom slot and place your check there. You can do this by hand or by right-clicking on your comboBox in Designer and selecting "Go to slot..." option.
-Argh sorry, didn't noticed that there's a code tag. I've edited it.-
-This was that, what I've been thought of, too. So it just loads while the window constructs. I'm a total newb to QT, could you explain this a bit more please? Where to put the code snippet then?-
Did it! Thanks
If you do it in Designer (Qt Creator), it will add the code automatically for you.
If you prefer to do it yourself, you need to add a slot to - for example - your MainWindow class:
void comboBoxChanged(const QString &newText);
Then you need to connect QComboBox signal to your new slot. I chose "currentIndexChanged" but you have many more to choose from (consult the docs):
// in MainWindow constructor or somewhere else if it fits your design better:
connect(ui->comboBox, SIGNAL(currentIndexChanged(QString)), this, SLOT(comboBoxChanged(QString)));
The last thing is to implement the new slot:
void MainWindow::comboBoxChanged(const QString &newText)
if (newText == "Digital IO")