Results 1 to 20 of 23

Thread: Changing text of button in no relation to button

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Sep 2010
    Location
    Israel
    Posts
    24
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Changing text of button in no relation to button

    How do I change the text of a PushButton with an action that has no relation to what the button does?
    Basically, I want the button to be pressed, some routines to run and if one of them does something right then the text on the button should change.

    I thought that just button.setText(qstring) will do the trick but it doesn't. I've tried following it up with update, repaint, paintEvent and a million other things I've found with Google searches but it doesn't work. The only thing I've thought of but haven't tried was to destroy and reload the entire UI.

    I could really use some help on this. Thanks.

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Changing text of button in no relation to button

    Always try to use the lowest common denominator.
    All objects of the main window are accessible via the main window, thus change the text of a component of the main window inside a function or slot of the main window.

    Example

    Qt Code:
    1. void MainWindow::slotButtonClicked()
    2. {
    3. int result = doSomething();
    4.  
    5. if (result == 1)
    6. button->setText("New text");
    7. }
    8.  
    9. int MainWindow::doSomething()
    10. {
    11. return something;
    12. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Sep 2010
    Location
    Israel
    Posts
    24
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Changing text of button in no relation to button

    But what if I need the ability to change the text of the button in a function that is triggered from a different button?

  4. #4
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Changing text of button in no relation to button

    You connect the second button's clicked() signal, with the slot that update the text of your first button.
    Read about signals and slots

  5. #5
    Join Date
    Sep 2010
    Location
    Israel
    Posts
    24
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Changing text of button in no relation to button

    I'm in a hurry to get some things done, stress from the bosses and everything, I can't delve too deeply into the mechanism right now.
    Why can't button.setText(text) just work as it is?

  6. #6
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Changing text of button in no relation to button

    It can.

    Against the rules, but here's an easy answer to your homework.

    Qt Code:
    1. class MainWindow : public something
    2. {
    3. Q_OBJECT
    4.  
    5. public slots:
    6. void button1Clicked();
    7. void button2Clicked();
    8.  
    9. private
    10. QPushButton *button1;
    11. QPushButton *button2;
    12. };
    13.  
    14. MainWindow::MainWindow(...) : ...
    15. {
    16. button1 = new ...;
    17. ...
    18.  
    19. connect(button1, SIGNAL(clicked()), this, slot(button1Clicked()));
    20. ...
    21. }
    22.  
    23. void MainWindow::button1Clicked()
    24. {
    25. button2->setText("Hello!");
    26. }
    27. ...
    To copy to clipboard, switch view to plain text mode 

    The real question, for me, is: what are you doing coding programs at a company if you don't know basic object oriented programming?

  7. #7
    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: Changing text of button in no relation to button

    Quote Originally Posted by Sabre Runner View Post
    Why can't button.setText(text) just work as it is?
    Please, show the code, which isn't working for you.

  8. #8
    Join Date
    Sep 2010
    Location
    Israel
    Posts
    24
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Changing text of button in no relation to button

    I'm a CS student and this is my first real job programming. Thanks for the vote of confidence.

    I need the ability to click a tool button, save the path taken so that other routines can use it.
    Qt Code:
    1. void SubNyquist::on_pushFindIC_clicked()
    2. {
    3. QString path = QDir::currentPath() += tr("/ic1.rbf");
    4. ICpath = QFileDialog::getOpenFileName(this,tr("Open IC File..."),
    5. path,tr("IC Files (*.rbf)"));
    6. ICpath.replace(tr("/"),tr("\\"));
    7. }
    To copy to clipboard, switch view to plain text mode 
    And when a certain function, triggered by a different button, finishes correctly, they want the path displayed on the button itself.
    Qt Code:
    1. void SubNyquist::on_pushLoadIC_clicked()
    2. {
    3. ...
    4. doSomething(Regarding the file given)
    5. ...
    6. // And if it doesn't break in the middle...
    7. pushFindIC.setText(ICpath);
    8. ...
    9. }
    To copy to clipboard, switch view to plain text mode 

  9. #9
    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: Changing text of button in no relation to button

    Quote Originally Posted by Sabre Runner View Post
    Qt Code:
    1. ICpath.replace(tr("/"),tr("\\"));
    To copy to clipboard, switch view to plain text mode 
    If you are a CS student, then here is the first lesson: Read the documentation! (QDir::toNativeSeparators)

    And are you sure pushFindIC is the right object? Because of your pices of code, I am not sure...

  10. #10
    Join Date
    Sep 2010
    Location
    Israel
    Posts
    24
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Changing text of button in no relation to button

    I do read the documentation but only the parts I think I need. I don't have the time to read the manual start to finish. Not right now.
    And how will that work as the path given to the file dialog?

    I have pushFindIC which is the Open dialog for the file to load. pushLoadIC is the button to actually load the file and do all the other things which it requires. If that doesn't break, for any reason, that is when I need to open button to change.

  11. #11
    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: Changing text of button in no relation to button

    Quote Originally Posted by Sabre Runner View Post
    And how will that work as the path given to the file dialog?
    It is static and takes a QString as argument?

    I have pushFindIC which is the Open dialog for the file to load. pushLoadIC is the button to actually load the file and do all the other things which it requires.
    Isn't pushFindIC your button? So where is the button where you want to change the text? And how do you call it? Are you using an ui file? (Seems so)

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Changing text of button in no relation to button

    Quote Originally Posted by Sabre Runner View Post
    I don't have the time to read the manual start to finish. Not right now.
    Saying such things will not get you going on this forum. Unless of course you want to hear that someone has no time to help you right now...

    You should really read about signals and slots. Learning some encapsulation and building object oriented modules would be very helpful too.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  13. #13
    Join Date
    Sep 2010
    Location
    Israel
    Posts
    24
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Changing text of button in no relation to button

    I'll do that. I will get that first on my list. I would still appreciate any help, if and when you have time.

    I am using a ui file. pushFindIC is the button for the open file dialog and the one I want to display the path in the text. pushLoadIC is the one which handles the processing of the file selected and if that process works, the path text should change.

Similar Threads

  1. Replies: 6
    Last Post: 21st August 2010, 21:09
  2. Replies: 1
    Last Post: 2nd August 2010, 05:40
  3. Button icon and text
    By electronicboy in forum Qt Programming
    Replies: 5
    Last Post: 9th October 2009, 22:27
  4. changing color of push button
    By babu198649 in forum Newbie
    Replies: 1
    Last Post: 2nd March 2008, 12:55

Tags for this Thread

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.