Results 1 to 4 of 4

Thread: How to checked if button is clicked

  1. #1
    Join Date
    May 2012
    Posts
    23
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default How to checked if button is clicked

    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?

  2. #2
    Join Date
    May 2012
    Posts
    136
    Thanks
    2
    Thanked 27 Times in 24 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to checked if button is clicked

    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
    Qt Code:
    1. private slots:
    2. void aButtonClicked();
    To copy to clipboard, switch view to plain text mode 

    this goes in the .cpp file
    Qt Code:
    1. Class::Class() //this is the constructor it may look different than the one you have
    2. {
    3. connect(ui->btnOne,SIGNAL(clicked()),this,SLOT(aButtonClicked()));
    4. connect(ui->btnTwo,SIGNAL(clicked()),this,SLOT(aButtonClicked()));
    5. }
    6.  
    7. void Class::aButtonClicked() //change "class" to your class its name
    8. {
    9. ui->btnThree->setEnabled(true);
    10. }
    To copy to clipboard, switch view to plain text mode 

    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
    Qt Code:
    1. private slots:
    2. void buttonOneClicked();
    3. void buttonTwoClicked();
    4.  
    5. private:
    6. void checkButton();
    7. bool chkBtnOne,chkBtnTwo;
    To copy to clipboard, switch view to plain text mode 

    this goes in the .cpp file
    Qt Code:
    1. Class::Class() //this is the constructor it may look different than the one you have
    2. {
    3. chkBtnOne = chkBtnTwo = false;
    4. connect(ui->btnOne,SIGNAL(clicked()),this,SLOT(buttonOneClicked()));
    5. connect(ui->btnTwo,SIGNAL(clicked()),this,SLOT(buttonTwoClicked()));
    6. }
    7.  
    8. void Class::buttonOneClicked() //change "class" to your class its name
    9. {
    10. chkBtnOne = true;
    11. checkButton();
    12. }
    13.  
    14. void Class::buttonTwoClicked() //change "class" to your class its name
    15. {
    16. chkBtnTwo = true;
    17. checkButton();
    18. }
    19.  
    20. void Class::checkButton()
    21. {
    22. if (chkBtnOne && chkBtnTwo)
    23. ui->btnThree->setEnabled(true);
    24. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by StrikeByte; 4th June 2012 at 15:00.

  3. #3
    Join Date
    May 2012
    Posts
    23
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to checked if button is clicked

    Can you give a simple example??


    Added after 5 minutes:


    StrikeByte thanks
    Last edited by stbb24; 4th June 2012 at 14:59.

  4. #4
    Join Date
    May 2012
    Location
    Bangalore, India
    Posts
    271
    Thanks
    29
    Thanked 50 Times in 47 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How to checked if button is clicked

    Here is the simple example:-

    widget.h
    Qt Code:
    1. #include <QWidget>
    2. #include<QVBoxLayout>
    3. #include<QPushButton>
    4.  
    5. class Widget : public QWidget
    6. {
    7. Q_OBJECT
    8.  
    9. public:
    10. explicit Widget(QWidget *parent = 0);
    11. ~Widget();
    12. public slots:
    13. void buttonAClicked();
    14. void buttonBClicked();
    15. private:
    16. int buttonA,buttonB;
    17. QWidget *widget;
    18. QVBoxLayout *verticalLayout;
    19. QPushButton *pushButton;
    20. QPushButton *pushButton_2;
    21. QPushButton *pushButton_3;
    22. };
    To copy to clipboard, switch view to plain text mode 


    widget.cpp
    Qt Code:
    1. #include "widget.h"
    2. #include<QApplication>
    3. Widget::Widget(QWidget *parent) :
    4. QWidget(parent)
    5. {
    6. buttonA=0;
    7. buttonB=0;
    8. this->resize(102, 116);
    9. this->setWindowTitle(QApplication::translate("Widget", "Widget", 0, QApplication::UnicodeUTF8));
    10.  
    11. widget = new QWidget(this);
    12. widget->setObjectName(QString::fromUtf8("widget"));
    13. widget->setGeometry(QRect(10, 10, 77, 83));
    14.  
    15.  
    16. verticalLayout = new QVBoxLayout(widget);
    17. verticalLayout->setSpacing(6);
    18. verticalLayout->setContentsMargins(11, 11, 11, 11);
    19. verticalLayout->setObjectName(QString::fromUtf8("verticalLayout"));
    20. verticalLayout->setContentsMargins(0, 0, 0, 0);
    21.  
    22.  
    23. pushButton = new QPushButton(widget);
    24. pushButton->setObjectName(QString::fromUtf8("pushButton"));
    25. pushButton->setText(QApplication::translate("Widget", "Button 1", 0, QApplication::UnicodeUTF8));
    26.  
    27. verticalLayout->addWidget(pushButton);
    28.  
    29. pushButton_2 = new QPushButton(widget);
    30. pushButton_2->setObjectName(QString::fromUtf8("pushButton_2"));
    31. pushButton_2->setText(QApplication::translate("Widget", "Button 2", 0, QApplication::UnicodeUTF8));
    32.  
    33. verticalLayout->addWidget(pushButton_2);
    34.  
    35. pushButton_3 = new QPushButton(widget);
    36. pushButton_3->setObjectName(QString::fromUtf8("pushButton_3"));
    37. pushButton_3->setEnabled(false);
    38. pushButton_3->setText(QApplication::translate("Widget", "Button 3", 0, QApplication::UnicodeUTF8));
    39.  
    40. verticalLayout->addWidget(pushButton_3);
    41.  
    42.  
    43.  
    44. connect(pushButton,SIGNAL(clicked()),this,SLOT(buttonAClicked()));
    45. connect(pushButton_2,SIGNAL(clicked()),this,SLOT(buttonBClicked()));
    46. }
    47.  
    48.  
    49. void Widget::buttonAClicked()
    50. {
    51.  
    52. buttonA=1;
    53. if(buttonB==1)
    54. {
    55. pushButton_3->setEnabled(true);
    56. }
    57. }
    58. void Widget::buttonBClicked()
    59. {
    60. buttonB=1;
    61. if(buttonA==1)
    62. {
    63. pushButton_3->setEnabled(true);
    64. }
    65. }
    66. Widget::~Widget()
    67. {
    68. }
    To copy to clipboard, switch view to plain text mode 

    main.cpp
    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include "widget.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. Widget w;
    8. w.show();
    9.  
    10. return a.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 


    Now when you will click either the button1 and button2 or vice versa then automatically button3 will get enable.

Similar Threads

  1. Replies: 1
    Last Post: 26th April 2012, 11:36
  2. How can find which button is clicked.
    By Niamita in forum Qt Programming
    Replies: 5
    Last Post: 27th June 2011, 15:35
  3. Replies: 2
    Last Post: 6th June 2011, 10:40
  4. How to get the clicked button handle?
    By Gokulnathvc in forum Newbie
    Replies: 1
    Last Post: 27th April 2011, 09:06
  5. Replies: 3
    Last Post: 26th July 2010, 03:23

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.