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

  2. #2
    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?

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

  4. #4
    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?

  5. #5
    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?

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

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

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

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

  10. #10
    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)

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,372
    Thanks
    3
    Thanked 5,019 Times in 4,795 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.


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

  13. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,372
    Thanks
    3
    Thanked 5,019 Times in 4,795 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.


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

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