Results 1 to 11 of 11

Thread: push button twice different text

  1. #1
    Join Date
    Sep 2016
    Posts
    8
    Qt products
    Qt4
    Platforms
    MacOS X

    Default push button twice different text

    Hi there,

    I have three push buttons in my project and I want them all to say when clicked once, "pump on" then when clicked again "pump off" could someone please teach me how to tell the push button to activate multiple times when pressed?

    Many thanks in advance.

    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include "QPixmap"
    4.  
    5. MainWindow::MainWindow(QWidget *parent) :
    6. QMainWindow(parent),
    7. ui(new Ui::MainWindow)
    8. {
    9. ui->setupUi(this);
    10. }
    11.  
    12. MainWindow::~MainWindow()
    13. {
    14. delete ui;
    15. }
    16.  
    17. void MainWindow::on_pushButton_clicked()
    18. {
    19. ui->label_5->setText("Pump On");
    20. }
    21.  
    22. void MainWindow::on_pushButton_2_clicked()
    23. {
    24. ui->label_6->setText("Pump On");
    25. }
    26.  
    27. void MainWindow::on_pushButton_3_clicked()
    28. {
    29. ui->label_7->setText("Pump On");
    30. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: push button twice different text

    If you want to change any button's text, call setText().

    What do you mean with "activate multiple times"?

    Cheers,
    _

  3. #3
    Join Date
    Sep 2016
    Posts
    8
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: push button twice different text

    Hi thanks for your reply,

    I understand how to set text and have successfully done that. But when i press the button again I want the text to change to "pump off"

    So press once, turns the label to "Pump on" press again "pump off"

  4. #4
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: push button twice different text

    Slot can looks like :
    Qt Code:
    1. void MainWindow::on_pushButton_clicked()
    2. {
    3. if( ui->label_5->text() == "Pump On" )
    4. ui->label_5->setText("Pump Off");
    5. else
    6. ui->label_5->setText("Pump On");
    7. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Sep 2016
    Posts
    8
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: push button twice different text

    That didn't work, also now i've input the new commands I get this error.

    linker command failed with exit code 1.

  6. #6
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: push button twice different text

    It has nothing to do with Qt.

  7. #7
    Join Date
    Sep 2016
    Posts
    8
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: push button twice different text

    Yep that was my peanut moment.. all fixed my bad.

    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4. MainWindow::MainWindow(QWidget *parent) :
    5. QMainWindow(parent),
    6. ui(new Ui::MainWindow)
    7. {
    8. ui->setupUi(this);
    9. }
    10.  
    11. MainWindow::~MainWindow()
    12. {
    13. delete ui;
    14. }
    15.  
    16. void MainWindow::on_Pump1_clicked()
    17. {
    18. if( ui->Pump1Label->text() == "Pump On")
    19. ui->Pump1Label->setText("Pump Off");
    20. else {
    21. ui->Pump1Label->setText("Pump On");
    22. }
    23. }
    24.  
    25. void MainWindow::on_Pump2_clicked()
    26. {
    27. if( ui->Pump2Label->text() == "Pump On")
    28. ui->Pump2Label->setText("Pump Off");
    29. else {
    30. ui->Pump2Label->setText("Pump On");
    31. }
    32. }
    33.  
    34. void MainWindow::on_Pump3_clicked()
    35. {
    36. if( ui->Pump3Label->text() == "Pump On")
    37. ui->Pump3Label->setText("Pump Off");
    38. else {
    39. ui->Pump3Label->setText("Pump On");
    40. }
    41. }
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: push button twice different text

    You realize that this code is not optimal? Much better would be such as :
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4. MainWindow::MainWindow(QWidget *parent) :
    5. QMainWindow(parent),
    6. ui(new Ui::MainWindow)
    7. {
    8. ui->setupUi(this);
    9. }
    10.  
    11. MainWindow::~MainWindow()
    12. {
    13. delete ui;
    14. }
    15.  
    16. void MainWindow::pumpLabelOnOff(QLabel *pl)
    17. {
    18. if( pl->text() == "Pump On")
    19. pl->setText("Pump Off");
    20. else {
    21. pl->setText("Pump On");
    22. }
    23. }
    24.  
    25. void MainWindow::on_Pump1_clicked()
    26. {
    27. pumpLabelOnOff(ui->Pump1Label);
    28. }
    29.  
    30. void MainWindow::on_Pump2_clicked()
    31. {
    32. pumpLabelOnOff(ui->Pump2Label);
    33. }
    34.  
    35. void MainWindow::on_Pump3_clicked()
    36. {
    37. pumpLabelOnOff(ui->Pump3Label);
    38. }
    To copy to clipboard, switch view to plain text mode 

  9. #9
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: push button twice different text

    If we're going to optimize, then depending on the actual text on the button to determine the pump state won't work when he has to port his software for a sale in Poland, will it?

    At least wrap the text in _tr(), or use a QObject dynamic property to store the pump state:

    Qt Code:
    1. void MainWindow::pumpLabelOnOff(QLabel *pl)
    2. {
    3. if ( pl->property( "PumpState" ).toString() == "On")
    4. {
    5. pl->setText( _tr("Pump Off") );
    6. pl->setProperty( "PumpState", QVariant( "Off" ) );
    7. }
    8. else
    9. {
    10. pl->setText( _tr("Pump On") );
    11. pl->setProperty( "PumpState", QVariant( "On" ) );
    12. }
    13. }
    To copy to clipboard, switch view to plain text mode 

    with the dynamic property being initialized when the button is constructed...
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  10. #10
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: push button twice different text

    For such a toggle state property I would actually recommend going with a bool as the value type

    Or with an int and a pre-filled array for the strings, something like

    Qt Code:
    1. QVector<QString> m_pumpTexts;
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. m_pumpTexts << tr("Pump Off") << tr("Pump On");
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void MainWindow::pumpLabelOnOff(QLabel *pl)
    2. {
    3. const int nextState = (pl->property("pumpState").toInt() + 1) % 2;
    4. pl->setText(m_pumpTexts[nextState]);
    5. pl->setProperty("pumpState", nextState);
    6. }
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  11. #11
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: push button twice different text

    Quote Originally Posted by d_stranz View Post
    If we're going to optimize, then depending on the actual text on the button to determine the pump state won't work when he has to port his software for a sale in Poland, will it?
    Of course, but we should not solve the whole problem for our friend Let's leave him something to do

  12. The following user says thank you to Lesiok for this useful post:

    d_stranz (3rd September 2016)

Similar Threads

  1. Replies: 1
    Last Post: 24th October 2015, 16:20
  2. push button
    By arumita in forum Newbie
    Replies: 1
    Last Post: 3rd June 2014, 01:02
  3. adding image and text both to a push button
    By Charvi in forum Qt Programming
    Replies: 5
    Last Post: 3rd August 2012, 08:44
  4. Replies: 1
    Last Post: 1st August 2011, 07:17
  5. How to change text color of push button?
    By augusbas in forum Qt Programming
    Replies: 2
    Last Post: 3rd July 2009, 10:32

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.