Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to prevent the mouse click event from happening when the button is Disabled
Forum Updated to NodeBB v4.3 + New Features

How to prevent the mouse click event from happening when the button is Disabled

Scheduled Pinned Locked Moved Unsolved General and Desktop
42 Posts 6 Posters 13.9k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • jsulmJ jsulm

    @ManiRon You mean this is the code in the slot connected to clicked() signal?
    What about my other questions? Can you answer them?

    ManiRonM Offline
    ManiRonM Offline
    ManiRon
    wrote on last edited by
    #9

    @jsulm I am using a style sheet , so i am not able to see whether the button is greyed out , i create a change in color when it is pressed , the color remains the same till the button is enabled

    jsulmJ 1 Reply Last reply
    0
    • ManiRonM ManiRon

      @jsulm I am using a style sheet , so i am not able to see whether the button is greyed out , i create a change in color when it is pressed , the color remains the same till the button is enabled

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #10

      @ManiRon Are you sure you're disabling the correct button?
      As I said: I can't reproduce such behaviour, there must be something in your code.
      Also you can disable your style sheet and test again.
      And to be sure: in the title you're talking about event, but you mean clicked() signal, correct?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      ManiRonM 1 Reply Last reply
      1
      • jsulmJ jsulm

        @ManiRon Are you sure you're disabling the correct button?
        As I said: I can't reproduce such behaviour, there must be something in your code.
        Also you can disable your style sheet and test again.
        And to be sure: in the title you're talking about event, but you mean clicked() signal, correct?

        ManiRonM Offline
        ManiRonM Offline
        ManiRon
        wrote on last edited by
        #11

        @jsulm Yes clicked signal

        1 Reply Last reply
        0
        • ManiRonM ManiRon

          I want to disable the click event for a QPushbutton when it is disabled, whether it can be done ?

          J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by
          #12

          @ManiRon
          can you create a minimal example?

          In fact you should!
          Or maybe @jsulm can share his for you ;)


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          jsulmJ 1 Reply Last reply
          0
          • J.HilkJ J.Hilk

            @ManiRon
            can you create a minimal example?

            In fact you should!
            Or maybe @jsulm can share his for you ;)

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #13

            @J.Hilk @ManiRon It's as simple as

            connect(ui->pushButton, &QPushButton::clicked, [this](bool){ qDebug() << "Clicked"; ui->pushButton->setDisabled(true);});
            

            And I'm still interested in an answer to this question: Are you sure you're disabling the correct button?

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            ManiRonM 2 Replies Last reply
            0
            • Pradeep P NP Offline
              Pradeep P NP Offline
              Pradeep P N
              wrote on last edited by Pradeep P N
              #14

              @jsulm @J-Hilk

              I think the problem here for @ManiRon is the style sheet for the QPushButton

              The Button is actually disabled on Click, but due to the StyleSheet the color difference is not found.

              
              void MainWindow::on_pushButton_clicked()
              {
                  qDebug() << Q_FUNC_INFO;
                  ui->label->setText("Button is Disabled");
                  ui->pushButton->setEnabled(false);
              }
              

              0_1561526021909_Btn.gif

              Pradeep Nimbalkar.
              Upvote the answer(s) that helped you to solve the issue...
              Keep code clean.

              1 Reply Last reply
              3
              • jsulmJ jsulm

                @J.Hilk @ManiRon It's as simple as

                connect(ui->pushButton, &QPushButton::clicked, [this](bool){ qDebug() << "Clicked"; ui->pushButton->setDisabled(true);});
                

                And I'm still interested in an answer to this question: Are you sure you're disabling the correct button?

                ManiRonM Offline
                ManiRonM Offline
                ManiRon
                wrote on last edited by
                #15

                @jsulm Yes Actually i am disabling the button for 3sec only so it accepts the signal again after 3sec , which i came to know when i printed the time with seconds

                1 Reply Last reply
                0
                • jsulmJ jsulm

                  @J.Hilk @ManiRon It's as simple as

                  connect(ui->pushButton, &QPushButton::clicked, [this](bool){ qDebug() << "Clicked"; ui->pushButton->setDisabled(true);});
                  

                  And I'm still interested in an answer to this question: Are you sure you're disabling the correct button?

                  ManiRonM Offline
                  ManiRonM Offline
                  ManiRon
                  wrote on last edited by
                  #16

                  @jsulm I think the problem here is the timeout that i have given , i think its less

                  Pradeep P NP 1 Reply Last reply
                  0
                  • ManiRonM ManiRon

                    @jsulm I think the problem here is the timeout that i have given , i think its less

                    Pradeep P NP Offline
                    Pradeep P NP Offline
                    Pradeep P N
                    wrote on last edited by Pradeep P N
                    #17

                    @ManiRon
                    I just tried with Timer it works fine.
                    The Button is disabled for 3s.

                    • Check with your stylesheet.
                    MainWindow::MainWindow(QWidget *parent) :
                        QMainWindow(parent),
                        ui(new Ui::MainWindow)
                    {
                        ui->setupUi(this);
                        ui->label->setVisible(false);
                    
                        tmr = new QTimer(this);
                        connect(tmr, &QTimer::timeout, [this]{
                            ui->pushButton->setEnabled(true);
                        });
                    }
                    
                    void MainWindow::on_pushButton_clicked()
                    {
                        qDebug() << Q_FUNC_INFO << QTime::currentTime().toString();
                    
                        tmr->start();
                        tmr->setInterval(3000);
                    
                        ui->pushButton->setEnabled(false);
                    }
                    

                    0_1561526880214_Btn.gif

                    Pradeep Nimbalkar.
                    Upvote the answer(s) that helped you to solve the issue...
                    Keep code clean.

                    ManiRonM 1 Reply Last reply
                    3
                    • Pradeep P NP Offline
                      Pradeep P NP Offline
                      Pradeep P N
                      wrote on last edited by Pradeep P N
                      #18

                      Hi @ManiRon

                      Try this with your setStyleSheet()

                      ui->pushButton->setStyleSheet("QPushButton {"
                                                    "background-color:#44c767;"
                                                    "}"
                                                    "QPushButton:disabled {"
                                                    "background-color:gray;"
                                                    "}");
                      

                      0_1561527474438_Btn.gif
                      All the best.

                      Pradeep Nimbalkar.
                      Upvote the answer(s) that helped you to solve the issue...
                      Keep code clean.

                      1 Reply Last reply
                      4
                      • Pradeep P NP Pradeep P N

                        @ManiRon
                        I just tried with Timer it works fine.
                        The Button is disabled for 3s.

                        • Check with your stylesheet.
                        MainWindow::MainWindow(QWidget *parent) :
                            QMainWindow(parent),
                            ui(new Ui::MainWindow)
                        {
                            ui->setupUi(this);
                            ui->label->setVisible(false);
                        
                            tmr = new QTimer(this);
                            connect(tmr, &QTimer::timeout, [this]{
                                ui->pushButton->setEnabled(true);
                            });
                        }
                        
                        void MainWindow::on_pushButton_clicked()
                        {
                            qDebug() << Q_FUNC_INFO << QTime::currentTime().toString();
                        
                            tmr->start();
                            tmr->setInterval(3000);
                        
                            ui->pushButton->setEnabled(false);
                        }
                        

                        0_1561526880214_Btn.gif

                        ManiRonM Offline
                        ManiRonM Offline
                        ManiRon
                        wrote on last edited by ManiRon
                        #19

                        @Pradeep-P-N @jsulm @J-Hilk
                        when i created a sample i was absorbing one thing , that when i click the button i disabled it , but while i click the button when it is disabled after enabling the button the click signal gets emitted and the click function is executed by the click signal which was occuring when i clicked the button when it was disabled

                        J.HilkJ 1 Reply Last reply
                        0
                        • ManiRonM ManiRon

                          @Pradeep-P-N @jsulm @J-Hilk
                          when i created a sample i was absorbing one thing , that when i click the button i disabled it , but while i click the button when it is disabled after enabling the button the click signal gets emitted and the click function is executed by the click signal which was occuring when i clicked the button when it was disabled

                          J.HilkJ Offline
                          J.HilkJ Offline
                          J.Hilk
                          Moderators
                          wrote on last edited by
                          #20

                          @ManiRon said in How to prevent the mouse click event from happening when the button is Disabled:

                          but while i click the button when it is disabled after enabling the button the click signal gets emitted and the click function is executed

                          If true, then that's a bug in the library!


                          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                          Q: What's that?
                          A: It's blue light.
                          Q: What does it do?
                          A: It turns blue.

                          ManiRonM Pradeep P NP J.HilkJ 3 Replies Last reply
                          0
                          • J.HilkJ J.Hilk

                            @ManiRon said in How to prevent the mouse click event from happening when the button is Disabled:

                            but while i click the button when it is disabled after enabling the button the click signal gets emitted and the click function is executed

                            If true, then that's a bug in the library!

                            ManiRonM Offline
                            ManiRonM Offline
                            ManiRon
                            wrote on last edited by
                            #21

                            @J.Hilk how it can be solved ?

                            jsulmJ 1 Reply Last reply
                            0
                            • J.HilkJ J.Hilk

                              @ManiRon said in How to prevent the mouse click event from happening when the button is Disabled:

                              but while i click the button when it is disabled after enabling the button the click signal gets emitted and the click function is executed

                              If true, then that's a bug in the library!

                              Pradeep P NP Offline
                              Pradeep P NP Offline
                              Pradeep P N
                              wrote on last edited by Pradeep P N
                              #22

                              @J.Hilk
                              i am bit confused with the statement in fact.

                              "but while i click the button when it is disabled after enabling the button the click signal gets emitted and the click function is executed"

                              • i click the button when it is disabled -> Nothing will work as Button is Disabled.
                              • after enabling the button the click signal gets emitted -> Yes as after 3s timer the button is enabled and can be clicked.

                              Pradeep Nimbalkar.
                              Upvote the answer(s) that helped you to solve the issue...
                              Keep code clean.

                              J.HilkJ 1 Reply Last reply
                              0
                              • Pradeep P NP Pradeep P N

                                @J.Hilk
                                i am bit confused with the statement in fact.

                                "but while i click the button when it is disabled after enabling the button the click signal gets emitted and the click function is executed"

                                • i click the button when it is disabled -> Nothing will work as Button is Disabled.
                                • after enabling the button the click signal gets emitted -> Yes as after 3s timer the button is enabled and can be clicked.
                                J.HilkJ Offline
                                J.HilkJ Offline
                                J.Hilk
                                Moderators
                                wrote on last edited by
                                #23

                                @Pradeep-P-N

                                What I get out of @ManiRon statement is:

                                • Button is disabled
                                • Button gets pressed/clicked
                                  • nothing happens, as expected
                                • Button gets set to enabled
                                  • clicked signal is emitted without button interaction

                                Thats not normal behavior in my opinion

                                I'll make a quick test case

                                @ManiRon what Qt Version what OS btw?


                                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                                Q: What's that?
                                A: It's blue light.
                                Q: What does it do?
                                A: It turns blue.

                                ManiRonM 1 Reply Last reply
                                1
                                • Pradeep P NP Offline
                                  Pradeep P NP Offline
                                  Pradeep P N
                                  wrote on last edited by
                                  #24

                                  Hmmm, Got it @J-Hilk
                                  Thanks.

                                  @ManiRon Can you share a sample code ?
                                  You said you are using the QTimer.
                                  Just wanted to check if there is anything mismatch with the code.

                                  Pradeep Nimbalkar.
                                  Upvote the answer(s) that helped you to solve the issue...
                                  Keep code clean.

                                  1 Reply Last reply
                                  0
                                  • J.HilkJ J.Hilk

                                    @ManiRon said in How to prevent the mouse click event from happening when the button is Disabled:

                                    but while i click the button when it is disabled after enabling the button the click signal gets emitted and the click function is executed

                                    If true, then that's a bug in the library!

                                    J.HilkJ Offline
                                    J.HilkJ Offline
                                    J.Hilk
                                    Moderators
                                    wrote on last edited by
                                    #25

                                    @J.Hilk said in How to prevent the mouse click event from happening when the button is Disabled:

                                    If true, then that's a bug in the library!

                                    Nope, not true 5.12.4 MacOS, no unexpected clicked emits

                                    int main(int argc, char *argv[])
                                    {
                                    
                                        QApplication a(argc, argv);
                                    
                                        auto *btn = new QPushButton("Enabled");
                                        btn->resize(200, 50);
                                        btn->show();
                                    
                                        QObject::connect(btn, &QPushButton::clicked, [btn]()->void{
                                                             btn->setEnabled(false);
                                                             btn->setText("Disabled");
                                                             qDebug("Btn clicked");
                                                         });
                                        QObject::connect(btn, &QPushButton::pressed, []()->void{qDebug("Btn Pressed"); });
                                        QObject::connect(btn, &QPushButton::released, []()->void{qDebug("Btn Released"); });
                                    
                                        QTimer t_reset;
                                        t_reset.setInterval(3000);
                                        QObject::connect(&t_reset, &QTimer::timeout, btn, [btn]()->void{btn->setEnabled(true); btn->setText("Enabled");});
                                    
                                        QObject::connect(btn, &QPushButton::clicked, &t_reset, QOverload<>::of(&QTimer::start));
                                    
                                        return  a.exec();
                                    }
                                    

                                    Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                                    Q: What's that?
                                    A: It's blue light.
                                    Q: What does it do?
                                    A: It turns blue.

                                    1 Reply Last reply
                                    3
                                    • Pradeep P NP Offline
                                      Pradeep P NP Offline
                                      Pradeep P N
                                      wrote on last edited by Pradeep P N
                                      #26

                                      @J-Hilk
                                      Even i tried on 5.12.3 - Ubuntu 16.04 & the above code ( From my post ).
                                      Also the one shared by you.
                                      It works fine.

                                      • No Click called related to button enable/disable status.

                                      Pradeep Nimbalkar.
                                      Upvote the answer(s) that helped you to solve the issue...
                                      Keep code clean.

                                      1 Reply Last reply
                                      3
                                      • ManiRonM ManiRon

                                        @J.Hilk how it can be solved ?

                                        jsulmJ Offline
                                        jsulmJ Offline
                                        jsulm
                                        Lifetime Qt Champion
                                        wrote on last edited by
                                        #27

                                        @ManiRon Please add

                                        qDebug() << ui->pushButton->enabled();
                                        

                                        to your slot and see what it outputs when you click on the button while it is disabled.
                                        And please debug you app more carefully: you say you have a timeout to activate the button again (you should have said that in your first post already)? Did you make sure the button was not yet activated? Is it the same if you disable you style sheet?

                                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                                        ManiRonM 1 Reply Last reply
                                        1
                                        • J.HilkJ J.Hilk

                                          @Pradeep-P-N

                                          What I get out of @ManiRon statement is:

                                          • Button is disabled
                                          • Button gets pressed/clicked
                                            • nothing happens, as expected
                                          • Button gets set to enabled
                                            • clicked signal is emitted without button interaction

                                          Thats not normal behavior in my opinion

                                          I'll make a quick test case

                                          @ManiRon what Qt Version what OS btw?

                                          ManiRonM Offline
                                          ManiRonM Offline
                                          ManiRon
                                          wrote on last edited by ManiRon
                                          #28

                                          @J.Hilk QT 5.5.0 QT Creator version 3.4.2 OS Windows Vista

                                          1 Reply Last reply
                                          0

                                          • Login

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved