Results 1 to 5 of 5

Thread: How to change QLabel value after Pushbutton pressed?

  1. #1
    Join Date
    Nov 2009
    Posts
    29
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default How to change QLabel value after Pushbutton pressed?

    Hello,

    I need to know how to change the value or text displayed in a QLabel after a PushButton is pressed.
    I have 2 buttons on my program. One PushButton is to trigger some math calculations. I want to display the result of the calculation after it is completed in a QLabel, but to display it only when I press the other PushButton. Then, if I do the math again, display the new result by pressing the other button and so on.

    ¿Can I use a PushButton clicked() signal to change a QLabel value?

    Thank you very much!!!!

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: How to change QLabel value after Pushbutton pressed?

    Yes, connect the clicked signal to a slot and in that slot you can set the text of your label.

  3. #3
    Join Date
    Nov 2009
    Posts
    29
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to change QLabel value after Pushbutton pressed?

    Hi,

    I did this:

    main.cpp

    Qt Code:
    1. #include <QApplication>
    2. #include <QLabel>
    3. #include <QPushButton>
    4. #include <QVBoxLayout>
    5. #include <QWidget>
    6. #include <QString>
    7.  
    8.  
    9. class MyWidget : public QWidget
    10. {
    11. public:
    12. MyWidget(QWidget *parent = 0);
    13.  
    14. QLabel *label;
    15. QString string;
    16.  
    17. signals:
    18.  
    19. public slots:
    20. void setTextLabel();
    21.  
    22. };
    23.  
    24. void MyWidget::setTextLabel()
    25. {
    26. label->setText("Test");
    27. }
    28.  
    29. MyWidget::MyWidget(QWidget *parent) :
    30. QWidget(parent)
    31. {
    32. QPushButton *quit = new QPushButton("Quit");
    33. QPushButton *showtext = new QPushButton("Show");
    34. label = new QLabel;
    35.  
    36. QObject::connect(quit, SIGNAL(clicked()), qApp, SLOT(quit()));
    37. QObject::connect(showtext, SIGNAL(clicked()), this, SLOT(setTextLabel()));
    38.  
    39. QVBoxLayout *layout = new QVBoxLayout;
    40.  
    41. layout->addWidget(label);
    42. layout->addWidget(showtext);
    43. layout->addWidget(quit);
    44. setLayout(layout);
    45. }
    46.  
    47.  
    48. int main(int argc, char *argv[])
    49. {
    50. QApplication app(argc, argv);
    51. MyWidget widget;
    52. widget.show();
    53. return app.exec();
    54. }
    To copy to clipboard, switch view to plain text mode 

    But it gives me the following Application Output error:

    Starting /home/simulab-3/Documentos/Qt/Training2010/Texto/Texto-build-desktop/Texto...
    Object::connect: No such slot QWidget::setTextLabel() in ../Texto/main.cpp:37
    So what is wrong?

    Thank you very much!!!

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: How to change QLabel value after Pushbutton pressed?

    Use the Q_OBJECT macro and put each class in a separate file or explicit include the moc file.

  5. The following user says thank you to Lykurg for this useful post:

    rdelgado (18th August 2010)

  6. #5
    Join Date
    Nov 2009
    Posts
    29
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to change QLabel value after Pushbutton pressed?

    Hi,

    Did the Q_OBJECT and the separate files and it worked! Thanks a lot!

Similar Threads

  1. QToolButton - doesn't change icons when pressed
    By walsha3000 in forum Qt Programming
    Replies: 1
    Last Post: 21st February 2010, 10:03
  2. Change QLabel to QLineEdit when mousePressEvent happens
    By naoyamakino in forum Qt Programming
    Replies: 2
    Last Post: 16th July 2009, 18:37
  3. Replies: 1
    Last Post: 2nd August 2008, 15:46
  4. Change color of a link in QLabel using Style Sheets?
    By codeslicer in forum Qt Programming
    Replies: 2
    Last Post: 15th April 2008, 11:00
  5. QLabel text change signal question
    By MarkoSan in forum Newbie
    Replies: 10
    Last Post: 5th April 2008, 10:19

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
  •  
Qt is a trademark of The Qt Company.