Page 1 of 2 12 LastLast
Results 1 to 20 of 23

Thread: Changing text of button in no relation to button

  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,364
    Thanks
    3
    Thanked 5,012 Times in 4,791 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.

  14. #14
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,012 Times in 4,791 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

    What exactly do you have problems with? Which part of your task you don't know how to do? You set button labels with manipulating the QAbstractButton::text property.
    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.


  15. #15
    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

    Yes. I did that. As far as I can tell, the text is there, saved in the button's property, it just won't display in the GUI on the button itself.

    My process, and I probably have no say in how this works, is that button 1 is pressed which opens the dialog to find the file. With file selected, button 2 is ready to go. Button 2 does some other things then executes a function. If that function returns correctly, then button 1's text should update to display the text it has.

    Everything seems to work up to that last part of updating or notifying button 1 that it needs to update.

    I'm reading the Signals and Slots tutorial now and trying to implement what I learn but so far no dice.

  16. #16
    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

    I guess at this stage we only can help you, if you provide a minimal compilable example reproducing your problem.

  17. #17
    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

    Qt Code:
    1. void Window::on_pushButton1_clicked()
    2. {
    3. path = QFileDialog::getOpenFileName(this, tr("Open File..."), path,tr("Files (*.*)"));
    4. path = QDir::toNativeSeparators(path);
    5. pushButton1.setText(path);
    6. }
    To copy to clipboard, switch view to plain text mode 
    This is the button that preps the path to be used to load the file from.
    Qt Code:
    1. void Window::on_pushButton2_clicked()
    2. {
    3. try something...
    4. catch one exception...
    5. {
    6. output error 1...
    7. }
    8. catch second exception...
    9. {
    10. output error 2...
    11. }
    12. catch all other exceptions...
    13. {
    14. output generic error...
    15. }
    16. QMessageBox::information(this,tr("Loaded"),tr("Loaded Successfully"));
    17. perform some other operations and configurations for the future...
    18. }
    To copy to clipboard, switch view to plain text mode 

    What I'd like to do is to change the text of button 1 if and only if button 2's operation has reached the loaded successfully part.

  18. #18
    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

    I was saying: compilable. The error must be somewhere in your design.

  19. #19
    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

    Am I forgetting to connect something? Is something unavailable? Is it because I'm using the UI designer? Are there several places were where signals and slots must be defined?

    Because I tried defining it with just a button release (hoping it will catch on one the button's operating would be done) signal to a set text slot in the designer and in the application start up and it hasn't worked. I can't seem to change anything in the UI from with in the code itself.

  20. #20
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,012 Times in 4,791 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

    Can you resize windows, click buttons and do all other stuff with your GUI after the button's text property is changed (and changes are not reflected on the button itself)? Maybe you are simply blocking the event loop?
    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.


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.