PDA

View Full Version : Problem with applying old values to interface items



sergee
8th July 2011, 12:06
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:



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".



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:



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.

mcosta
8th July 2011, 17:02
For me this code



#include <QtGui/QApplication>
#include <QLabel>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QLabel w;
w.setText ("1");
w.setText ("2");
w.setText ("1");
w.show();

return a.exec();
}

ad this



Widget1::Widget1(QWidget *parent) :
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?

sergee
8th July 2011, 19:10
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:



class mf : public QWidget
{
Q_OBJECT

public:
mf(QWidget *parent = 0);
~mf();

private:
Ui::Form ui;
private slots:
void on_pushButton_clicked();
};

mf::mf(QWidget *parent)
: QWidget(parent)
{
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.

Lesiok
9th July 2011, 08:19
You must read about connecting slots by name (http://doc.trolltech.com/4.7/qmetaobject.html#connectSlotsByName). 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.