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
QtWS25 Last Chance

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.8k 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.
  • M Offline
    M Offline
    ManiRon
    wrote on 25 Jun 2019, 12:24 last edited by
    #1

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

    J J 2 Replies Last reply 25 Jun 2019, 12:43
    0
    • M ManiRon
      25 Jun 2019, 12:24

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

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 25 Jun 2019, 12:43 last edited by
      #2

      @ManiRon If a QPushButton is disabled nothing happens if you click on it, so what do you want to disable?

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

      M 1 Reply Last reply 26 Jun 2019, 04:20
      5
      • J jsulm
        25 Jun 2019, 12:43

        @ManiRon If a QPushButton is disabled nothing happens if you click on it, so what do you want to disable?

        M Offline
        M Offline
        ManiRon
        wrote on 26 Jun 2019, 04:20 last edited by
        #3

        @jsulm even when the push button is disabled, when we click on the disabled QPushButton the click signal is generated and operations are performed again . which i want to stop .

        J 1 Reply Last reply 26 Jun 2019, 04:33
        0
        • M ManiRon
          26 Jun 2019, 04:20

          @jsulm even when the push button is disabled, when we click on the disabled QPushButton the click signal is generated and operations are performed again . which i want to stop .

          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 26 Jun 2019, 04:33 last edited by
          #4

          @ManiRon No, clicked() signal is not emitted if the button is disabled. This is the whole point of disabling a button. And I just tested it.
          How did you disable the button?

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

          M 1 Reply Last reply 26 Jun 2019, 04:34
          2
          • J jsulm
            26 Jun 2019, 04:33

            @ManiRon No, clicked() signal is not emitted if the button is disabled. This is the whole point of disabling a button. And I just tested it.
            How did you disable the button?

            M Offline
            M Offline
            ManiRon
            wrote on 26 Jun 2019, 04:34 last edited by
            #5

            @jsulm

              ui->pushbutton->setdisabled(true);
            

            I disable the pushbutton once it is clicked , but the signal was still created

            J 1 Reply Last reply 26 Jun 2019, 04:37
            0
            • M ManiRon
              26 Jun 2019, 04:34

              @jsulm

                ui->pushbutton->setdisabled(true);
              

              I disable the pushbutton once it is clicked , but the signal was still created

              J Offline
              J Offline
              jsulm
              Lifetime Qt Champion
              wrote on 26 Jun 2019, 04:37 last edited by
              #6

              @ManiRon Just did the same: disabled button after first click. Works as expected for me: after disabling clicked signal is not emitted. Is the button shown as greyed out after disabling? Are you sure you're disabling the correct button?
              By the way: it is setDisabled not setdisabled.

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

              M 1 Reply Last reply 26 Jun 2019, 04:49
              2
              • J jsulm
                26 Jun 2019, 04:37

                @ManiRon Just did the same: disabled button after first click. Works as expected for me: after disabling clicked signal is not emitted. Is the button shown as greyed out after disabling? Are you sure you're disabling the correct button?
                By the way: it is setDisabled not setdisabled.

                M Offline
                M Offline
                ManiRon
                wrote on 26 Jun 2019, 04:49 last edited by ManiRon
                #7

                @jsulm

                 ui->label->clear();
                ui->label->repaint();
                  ui->pushbutton->setDisabled(true);
                   TargetConnect();
                

                This is my exact code when i click the button

                J 1 Reply Last reply 26 Jun 2019, 04:51
                0
                • M ManiRon
                  26 Jun 2019, 04:49

                  @jsulm

                   ui->label->clear();
                  ui->label->repaint();
                    ui->pushbutton->setDisabled(true);
                     TargetConnect();
                  

                  This is my exact code when i click the button

                  J Offline
                  J Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on 26 Jun 2019, 04:51 last edited by jsulm
                  #8

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

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

                  M 1 Reply Last reply 26 Jun 2019, 04:53
                  0
                  • J jsulm
                    26 Jun 2019, 04:51

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

                    M Offline
                    M Offline
                    ManiRon
                    wrote on 26 Jun 2019, 04:53 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

                    J 1 Reply Last reply 26 Jun 2019, 04:55
                    0
                    • M ManiRon
                      26 Jun 2019, 04:53

                      @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

                      J Offline
                      J Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on 26 Jun 2019, 04:55 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

                      M 1 Reply Last reply 26 Jun 2019, 04:56
                      1
                      • J jsulm
                        26 Jun 2019, 04:55

                        @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?

                        M Offline
                        M Offline
                        ManiRon
                        wrote on 26 Jun 2019, 04:56 last edited by
                        #11

                        @jsulm Yes clicked signal

                        1 Reply Last reply
                        0
                        • M ManiRon
                          25 Jun 2019, 12:24

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

                          J Offline
                          J Offline
                          J.Hilk
                          Moderators
                          wrote on 26 Jun 2019, 04:59 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.

                          J 1 Reply Last reply 26 Jun 2019, 05:04
                          0
                          • J J.Hilk
                            26 Jun 2019, 04:59

                            @ManiRon
                            can you create a minimal example?

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

                            J Offline
                            J Offline
                            jsulm
                            Lifetime Qt Champion
                            wrote on 26 Jun 2019, 05:04 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

                            M 2 Replies Last reply 26 Jun 2019, 05:12
                            0
                            • Pradeep P NP Offline
                              Pradeep P NP Offline
                              Pradeep P N
                              wrote on 26 Jun 2019, 05:12 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
                              • J jsulm
                                26 Jun 2019, 05:04

                                @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?

                                M Offline
                                M Offline
                                ManiRon
                                wrote on 26 Jun 2019, 05:12 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
                                • J jsulm
                                  26 Jun 2019, 05:04

                                  @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?

                                  M Offline
                                  M Offline
                                  ManiRon
                                  wrote on 26 Jun 2019, 05:14 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 26 Jun 2019, 05:22
                                  0
                                  • M ManiRon
                                    26 Jun 2019, 05:14

                                    @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 26 Jun 2019, 05:22 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.

                                    M 1 Reply Last reply 26 Jun 2019, 05:37
                                    3
                                    • Pradeep P NP Offline
                                      Pradeep P NP Offline
                                      Pradeep P N
                                      wrote on 26 Jun 2019, 05:37 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
                                        26 Jun 2019, 05:22

                                        @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

                                        M Offline
                                        M Offline
                                        ManiRon
                                        wrote on 26 Jun 2019, 05:37 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 1 Reply Last reply 26 Jun 2019, 05:41
                                        0
                                        • M ManiRon
                                          26 Jun 2019, 05:37

                                          @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 Offline
                                          J Offline
                                          J.Hilk
                                          Moderators
                                          wrote on 26 Jun 2019, 05:41 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.

                                          M Pradeep P NP J 3 Replies Last reply 26 Jun 2019, 05:43
                                          0

                                          3/42

                                          26 Jun 2019, 04:20

                                          topic:navigator.unread, 39
                                          • Login

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