View Full Version : How to checked if button is clicked
stbb24
4th June 2012, 13:38
I have three buttons the 3rd button is disable by default the user will only be able to clicked it if the first two buttons are clicked.
So how do I checked if the first two buttons was clicked to enable the third button?
StrikeByte
4th June 2012, 13:43
You have to create a slot and connect the clicked signal from both the buttons to it.
in the slot you can enable the third button
this needs to be in the .h file
private slots:
void aButtonClicked();
this goes in the .cpp file
Class::Class() //this is the constructor it may look different than the one you have
{
connect(ui->btnOne,SIGNAL(clicked()),this,SLOT(aButtonClicked( )));
connect(ui->btnTwo,SIGNAL(clicked()),this,SLOT(aButtonClicked( )));
}
void Class::aButtonClicked() //change "class" to your class its name
{
ui->btnThree->setEnabled(true);
}
this piece of code enables the third button if one of the two buttons has been clicked
if both of the buttons have to be clicked before enabeling the third then it needs some extra code
this needs to be in the .h file
private slots:
void buttonOneClicked();
void buttonTwoClicked();
private:
void checkButton();
bool chkBtnOne,chkBtnTwo;
this goes in the .cpp file
Class::Class() //this is the constructor it may look different than the one you have
{
chkBtnOne = chkBtnTwo = false;
connect(ui->btnOne,SIGNAL(clicked()),this,SLOT(buttonOneClicke d()));
connect(ui->btnTwo,SIGNAL(clicked()),this,SLOT(buttonTwoClicke d()));
}
void Class::buttonOneClicked() //change "class" to your class its name
{
chkBtnOne = true;
checkButton();
}
void Class::buttonTwoClicked() //change "class" to your class its name
{
chkBtnTwo = true;
checkButton();
}
void Class::checkButton()
{
if (chkBtnOne && chkBtnTwo)
ui->btnThree->setEnabled(true);
}
stbb24
4th June 2012, 13:59
Can you give a simple example??
Added after 5 minutes:
StrikeByte thanks :)
sonulohani
5th June 2012, 06:04
Here is the simple example:-
widget.h
#include <QWidget>
#include<QVBoxLayout>
#include<QPushButton>
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
public slots:
void buttonAClicked();
void buttonBClicked();
private:
int buttonA,buttonB;
QWidget *widget;
QVBoxLayout *verticalLayout;
QPushButton *pushButton;
QPushButton *pushButton_2;
QPushButton *pushButton_3;
};
widget.cpp
#include "widget.h"
#include<QApplication>
Widget::Widget(QWidget *parent) :
QWidget(parent)
{
buttonA=0;
buttonB=0;
this->resize(102, 116);
this->setWindowTitle(QApplication::translate("Widget", "Widget", 0, QApplication::UnicodeUTF8));
widget = new QWidget(this);
widget->setObjectName(QString::fromUtf8("widget"));
widget->setGeometry(QRect(10, 10, 77, 83));
verticalLayout = new QVBoxLayout(widget);
verticalLayout->setSpacing(6);
verticalLayout->setContentsMargins(11, 11, 11, 11);
verticalLayout->setObjectName(QString::fromUtf8("verticalLayout"));
verticalLayout->setContentsMargins(0, 0, 0, 0);
pushButton = new QPushButton(widget);
pushButton->setObjectName(QString::fromUtf8("pushButton"));
pushButton->setText(QApplication::translate("Widget", "Button 1", 0, QApplication::UnicodeUTF8));
verticalLayout->addWidget(pushButton);
pushButton_2 = new QPushButton(widget);
pushButton_2->setObjectName(QString::fromUtf8("pushButton_2"));
pushButton_2->setText(QApplication::translate("Widget", "Button 2", 0, QApplication::UnicodeUTF8));
verticalLayout->addWidget(pushButton_2);
pushButton_3 = new QPushButton(widget);
pushButton_3->setObjectName(QString::fromUtf8("pushButton_3"));
pushButton_3->setEnabled(false);
pushButton_3->setText(QApplication::translate("Widget", "Button 3", 0, QApplication::UnicodeUTF8));
verticalLayout->addWidget(pushButton_3);
connect(pushButton,SIGNAL(clicked()),this,SLOT(but tonAClicked()));
connect(pushButton_2,SIGNAL(clicked()),this,SLOT(b uttonBClicked()));
}
void Widget::buttonAClicked()
{
buttonA=1;
if(buttonB==1)
{
pushButton_3->setEnabled(true);
}
}
void Widget::buttonBClicked()
{
buttonB=1;
if(buttonA==1)
{
pushButton_3->setEnabled(true);
}
}
Widget::~Widget()
{
}
main.cpp
#include <QtGui/QApplication>
#include "widget.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
Now when you will click either the button1 and button2 or vice versa then automatically button3 will get enable.
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.