Results 1 to 4 of 4

Thread: How to checked if button is clicked

Hybrid View

Previous Post Previous Post   Next Post Next Post
  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 14:00.

Similar Threads

  1. Replies: 1
    Last Post: 26th April 2012, 10:36
  2. How can find which button is clicked.
    By Niamita in forum Qt Programming
    Replies: 5
    Last Post: 27th June 2011, 14:35
  3. Replies: 2
    Last Post: 6th June 2011, 09:40
  4. How to get the clicked button handle?
    By Gokulnathvc in forum Newbie
    Replies: 1
    Last Post: 27th April 2011, 08:06
  5. Replies: 3
    Last Post: 26th July 2010, 02: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
  •  
Qt is a trademark of The Qt Company.