Problem with applying old values to interface items
Hello everybody!
I am new to QT, probably i'm missing some basic concept, i wrote a simple demo app (windows/mingw), i am trying to change caption on push button, it works for the first time, but if i'm trying to set old value after that - it doesn't. Here are some examples:
Code:
this->ui.pushButton->setText("1");
this->ui.pushButton->setText("2");
this->ui.pushButton->setText("3");
this works fine, the value displayed on button is "3".
Code:
this->ui.pushButton->setText("1");
this->ui.pushButton->setText("2");
this->ui.pushButton->setText("1");
This example doesn't work as i expect - the displayed value on the button is "2".
similar situation with other properties, for example:
Code:
this->ui.spinBox->setEnabled(true);
this->ui.spinBox->setEnabled(false);
this->ui.spinBox->setEnabled(true);
the element stays in "disabled" state.
i tryed to call update() after each property change for the particular element - that didn't help.
Any suggestions? what am i missing here?
Thank you all in advance.
Re: Problem with applying old values to interface items
For me this code
Code:
#include <QtGui/QApplication>
#include <QLabel>
int main(int argc, char *argv[])
{
w.setText ("1");
w.setText ("2");
w.setText ("1");
w.show();
return a.exec();
}
ad this
Code:
Widget1
::Widget1(QWidget *parent
) : ui(new Ui::Widget1)
{
ui->setupUi(this);
ui->loadInfoButton->setText ("1");
ui->loadInfoButton->setText ("2");
ui->loadInfoButton->setText ("1");
}
work fine.
What Qt version are you using?
Re: Problem with applying old values to interface items
The source of this problem is the name of slot which handles click on push button. Here is a code sample that doesn't work:
Code:
{
Q_OBJECT
public:
~mf();
private:
Ui::Form ui;
private slots:
void on_pushButton_clicked();
};
{
ui.setupUi(this);
QObject::connect(ui.
pushButton,
SIGNAL(clicked
()),
this,
SLOT(on_pushButton_clicked
()));
}
void mf::on_pushButton_clicked() {
if (this->ui.pushButton->text() == "Start") {
this->ui.pushButton->setText("Stop");
this->ui.spinBox->setEnabled(false);
} else {
this->ui.pushButton->setText("Start");
this->ui.spinBox->setEnabled(true);
}
}
If i change the name of the signal handler (on_pushButton_clicked) or remove manual signal assignment - everything works fine. Looks like on_pushButton_clicked was called twice very fast - that's why i thought that labels and properties weren't updated.
Thank you very much everybody :) Sorry for stupid question.
Re: Problem with applying old values to interface items
You must read about connecting slots by name. This slot is called twice because it is connected twice. First connection is maked automatically in setupUi() and You are making connection second time manually.