PDA

View Full Version : push button twice different text



Kloster
1st September 2016, 07:39
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.


#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "QPixmap"

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::on_pushButton_clicked()
{
ui->label_5->setText("Pump On");
}

void MainWindow::on_pushButton_2_clicked()
{
ui->label_6->setText("Pump On");
}

void MainWindow::on_pushButton_3_clicked()
{
ui->label_7->setText("Pump On");
}

anda_skoa
1st September 2016, 09:32
If you want to change any button's text, call setText().

What do you mean with "activate multiple times"?

Cheers,
_

Kloster
1st September 2016, 09:36
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"

Lesiok
1st September 2016, 10:00
Slot can looks like :
void MainWindow::on_pushButton_clicked()
{
if( ui->label_5->text() == "Pump On" )
ui->label_5->setText("Pump Off");
else
ui->label_5->setText("Pump On");
}

Kloster
1st September 2016, 10:17
That didn't work, also now i've input the new commands I get this error.

linker command failed with exit code 1.

Lesiok
1st September 2016, 11:36
It has nothing to do with Qt.

Kloster
2nd September 2016, 11:01
Yep that was my peanut moment.. all fixed my bad.


#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::on_Pump1_clicked()
{
if( ui->Pump1Label->text() == "Pump On")
ui->Pump1Label->setText("Pump Off");
else {
ui->Pump1Label->setText("Pump On");
}
}

void MainWindow::on_Pump2_clicked()
{
if( ui->Pump2Label->text() == "Pump On")
ui->Pump2Label->setText("Pump Off");
else {
ui->Pump2Label->setText("Pump On");
}
}

void MainWindow::on_Pump3_clicked()
{
if( ui->Pump3Label->text() == "Pump On")
ui->Pump3Label->setText("Pump Off");
else {
ui->Pump3Label->setText("Pump On");
}
}

Lesiok
2nd September 2016, 11:57
You realize that this code is not optimal? Much better would be such as :
#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::pumpLabelOnOff(QLabel *pl)
{
if( pl->text() == "Pump On")
pl->setText("Pump Off");
else {
pl->setText("Pump On");
}
}

void MainWindow::on_Pump1_clicked()
{
pumpLabelOnOff(ui->Pump1Label);
}

void MainWindow::on_Pump2_clicked()
{
pumpLabelOnOff(ui->Pump2Label);
}

void MainWindow::on_Pump3_clicked()
{
pumpLabelOnOff(ui->Pump3Label);
}

d_stranz
2nd September 2016, 17:29
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:



void MainWindow::pumpLabelOnOff(QLabel *pl)
{
if ( pl->property( "PumpState" ).toString() == "On")
{
pl->setText( _tr("Pump Off") );
pl->setProperty( "PumpState", QVariant( "Off" ) );
}
else
{
pl->setText( _tr("Pump On") );
pl->setProperty( "PumpState", QVariant( "On" ) );
}
}

with the dynamic property being initialized when the button is constructed...

anda_skoa
2nd September 2016, 17:58
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



QVector<QString> m_pumpTexts;



m_pumpTexts << tr("Pump Off") << tr("Pump On");




void MainWindow::pumpLabelOnOff(QLabel *pl)
{
const int nextState = (pl->property("pumpState").toInt() + 1) % 2;
pl->setText(m_pumpTexts[nextState]);
pl->setProperty("pumpState", nextState);
}


Cheers,
_

Lesiok
2nd September 2016, 18:17
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 :)