Results 1 to 4 of 4

Thread: Problem with applying old values to interface items

  1. #1
    Join Date
    Jul 2011
    Posts
    2
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default 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:

    Qt Code:
    1. this->ui.pushButton->setText("1");
    2. this->ui.pushButton->setText("2");
    3. this->ui.pushButton->setText("3");
    To copy to clipboard, switch view to plain text mode 

    this works fine, the value displayed on button is "3".

    Qt Code:
    1. this->ui.pushButton->setText("1");
    2. this->ui.pushButton->setText("2");
    3. this->ui.pushButton->setText("1");
    To copy to clipboard, switch view to plain text mode 

    This example doesn't work as i expect - the displayed value on the button is "2".


    similar situation with other properties, for example:

    Qt Code:
    1. this->ui.spinBox->setEnabled(true);
    2. this->ui.spinBox->setEnabled(false);
    3. this->ui.spinBox->setEnabled(true);
    To copy to clipboard, switch view to plain text mode 

    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.

  2. #2
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with applying old values to interface items

    For me this code

    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include <QLabel>
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. QLabel w;
    8. w.setText ("1");
    9. w.setText ("2");
    10. w.setText ("1");
    11. w.show();
    12.  
    13. return a.exec();
    14. }
    To copy to clipboard, switch view to plain text mode 

    ad this

    Qt Code:
    1. Widget1::Widget1(QWidget *parent) :
    2. QWidget(parent),
    3. ui(new Ui::Widget1)
    4. {
    5. ui->setupUi(this);
    6.  
    7. ui->loadInfoButton->setText ("1");
    8. ui->loadInfoButton->setText ("2");
    9. ui->loadInfoButton->setText ("1");
    10. }
    To copy to clipboard, switch view to plain text mode 

    work fine.

    What Qt version are you using?
    A camel can go 14 days without drink,
    I can't!!!

  3. The following user says thank you to mcosta for this useful post:

    sergee (8th July 2011)

  4. #3
    Join Date
    Jul 2011
    Posts
    2
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default 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:

    Qt Code:
    1. class mf : public QWidget
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. mf(QWidget *parent = 0);
    7. ~mf();
    8.  
    9. private:
    10. Ui::Form ui;
    11. private slots:
    12. void on_pushButton_clicked();
    13. };
    14.  
    15. mf::mf(QWidget *parent)
    16. : QWidget(parent)
    17. {
    18. ui.setupUi(this);
    19. QObject::connect(ui.pushButton, SIGNAL(clicked()), this, SLOT(on_pushButton_clicked()));
    20. }
    21.  
    22. void mf::on_pushButton_clicked() {
    23. if (this->ui.pushButton->text() == "Start") {
    24. this->ui.pushButton->setText("Stop");
    25. this->ui.spinBox->setEnabled(false);
    26. } else {
    27. this->ui.pushButton->setText("Start");
    28. this->ui.spinBox->setEnabled(true);
    29. }
    30. }
    To copy to clipboard, switch view to plain text mode 

    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.
    Last edited by sergee; 8th July 2011 at 20:14.

  5. #4
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

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

  6. The following user says thank you to Lesiok for this useful post:

    sergee (10th July 2011)

Similar Threads

  1. Replies: 1
    Last Post: 23rd April 2011, 18:33
  2. Replies: 3
    Last Post: 22nd August 2010, 10:23
  3. Problem with design interface
    By tux-world in forum Newbie
    Replies: 5
    Last Post: 10th March 2010, 15:19
  4. Replies: 8
    Last Post: 1st October 2009, 11:07
  5. Problem applying setWindowOpacity to a custom Widget
    By yellowmat in forum Qt Programming
    Replies: 8
    Last Post: 1st November 2006, 11:05

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.