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?
Printable View
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?
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
Code:
private slots: void aButtonClicked();
this goes in the .cpp file
Code:
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
Code:
private slots: void buttonOneClicked(); void buttonTwoClicked(); private: void checkButton(); bool chkBtnOne,chkBtnTwo;
this goes in the .cpp file
Code:
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(buttonOneClicked())); connect(ui->btnTwo,SIGNAL(clicked()),this,SLOT(buttonTwoClicked())); } 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); }
Can you give a simple example??
Added after 5 minutes:
StrikeByte thanks :)
Here is the simple example:-
widget.h
Code:
#include <QWidget> #include<QVBoxLayout> #include<QPushButton> { Q_OBJECT public: ~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
Code:
#include "widget.h" #include<QApplication> { buttonA=0; buttonB=0; this->resize(102, 116); verticalLayout->setSpacing(6); verticalLayout->setContentsMargins(11, 11, 11, 11); verticalLayout->setContentsMargins(0, 0, 0, 0); verticalLayout->addWidget(pushButton); verticalLayout->addWidget(pushButton_2); pushButton_3->setEnabled(false); verticalLayout->addWidget(pushButton_3); connect(pushButton,SIGNAL(clicked()),this,SLOT(buttonAClicked())); connect(pushButton_2,SIGNAL(clicked()),this,SLOT(buttonBClicked())); } 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
Code:
#include <QtGui/QApplication> #include "widget.h" int main(int argc, char *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.