Results 1 to 4 of 4

Thread: changing QPushButton label

  1. #1
    Join Date
    Dec 2009
    Location
    New Zealand
    Posts
    54
    Thanks
    23
    Qt products
    Qt4
    Platforms
    Windows

    Default changing QPushButton label

    Hi,

    I am trying to find a way to change the text displayed on a button, currently I have created a button:

    Qt Code:
    1. QPushButton *uploadButton = new QPushButton(tr("Upload To Unit number one"));
    To copy to clipboard, switch view to plain text mode 

    and once I have clicked this button, I would like the button label to be changed to something like

    Qt Code:
    1. "Upload To Unit number two"
    To copy to clipboard, switch view to plain text mode 


    I know I can use something like:

    Qt Code:
    1. uploadButton->setText("upload to unit number two");
    To copy to clipboard, switch view to plain text mode 

    But when I put this in a slot that is activated by the signal emitted by the uploadButton, my application just quits,

    I think maybe I have to declare the uploadButton as a "Global" widget( if you know what I mean), but I'm not sure how to do this..


    Any advice appreciated, Thanks

  2. #2
    Join Date
    Jul 2009
    Location
    Enschede, Netherlands
    Posts
    462
    Thanked 69 Times in 67 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: changing QPushButton label

    Put it in your class definition:
    Qt Code:
    1. class YourWidget : public QWidget {
    2. public:
    3. YourWidget();
    4. virtual ~YourWidget();
    5.  
    6. protected slots:
    7. void updateTexts();
    8.  
    9. private:
    10. QPushButton *m_pushButton;
    11. };
    12.  
    13. void YourWidget::updateTexts()
    14. {
    15. m_pushButton->setText("New text");
    16. }
    To copy to clipboard, switch view to plain text mode 
    Another possibility is, since you know what is calling the function:
    Qt Code:
    1. void YourWidget::updateTexts()
    2. {
    3. QPushButton *caller = qobject_cast<QPushButton *>(sender());
    4. if (!caller) // caller is NULL if sender() cannot be cast to the requested type.
    5. return;
    6. caller->setText("New text");
    7. }
    To copy to clipboard, switch view to plain text mode 
    Horse sense is the thing that keeps horses from betting on people. --W.C. Fields

    Ask Smart Questions

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

    Ferric (31st January 2010)

  4. #3
    Join Date
    Dec 2009
    Location
    New Zealand
    Posts
    54
    Thanks
    23
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: changing QPushButton label

    I did the following as suggested,

    In the header file:
    Qt Code:
    1. protected slots:
    2.  
    3. void updateTexts();
    4.  
    5.  
    6. private:
    7.  
    8. QPushButton *m_pushButton;
    To copy to clipboard, switch view to plain text mode 

    In the source file:
    Qt Code:
    1. void YourWidget::updateTexts()
    2.  
    3. {
    4.  
    5. m_pushButton->setText("New text");
    6.  
    7. }
    To copy to clipboard, switch view to plain text mode 


    Then I attached the button signal to the updateTexts() slot, my program built and ran, but when I clicked the button, windows just says "myprogram.exe has encountered a problem and needs to close. We are sorry for the inconvenience."

    I might try getting this concept working on a simple example first, basically if I can find a button that updates its own label every time its clicked I should be able to work backwards to implement it in my program. If anyone can think of a concept like this in any of the Qt Examples that are included with Qt creator please let me know.
    Last edited by Ferric; 31st January 2010 at 10:15.

  5. #4
    Join Date
    Dec 2009
    Location
    New Zealand
    Posts
    54
    Thanks
    23
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: changing QPushButton label

    Aha, I got it, I just had to create the button in the following manner:

    Qt Code:
    1. uploadButton = new QPushButton;
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 2
    Last Post: 10th August 2009, 10:45
  2. Changing QPushButton text colour with mouseMoveEvent
    By Misenko in forum Qt Programming
    Replies: 1
    Last Post: 10th June 2008, 17:53
  3. label Text is not changing using Qt Linguist
    By thomasjoy in forum Qt Tools
    Replies: 59
    Last Post: 8th October 2007, 16:24
  4. changing QLabel font size(label created on the graphicsView)
    By maverick_pol in forum Qt Programming
    Replies: 11
    Last Post: 17th August 2007, 09:36
  5. Replies: 3
    Last Post: 26th September 2006, 13:16

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.