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 14.2k 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.
  • 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
      • jsulmJ jsulm

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

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

        @jsulm Yes i made sure cause i enable the button after 3s only , and this statement is not there , it throws error (qDebug() << ui->pushButton->enabled())

        Pradeep P NP 1 Reply Last reply
        0
        • ManiRonM ManiRon

          @jsulm Yes i made sure cause i enable the button after 3s only , and this statement is not there , it throws error (qDebug() << ui->pushButton->enabled())

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

          @ManiRon
          Did you add #include <QDebug>

          Ad if so -> What is the error ?

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

          ManiRonM 2 Replies Last reply
          3
          • Pradeep P NP Pradeep P N

            @ManiRon
            Did you add #include <QDebug>

            Ad if so -> What is the error ?

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

            @Pradeep-P-N yes

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

              @ManiRon
              Did you add #include <QDebug>

              Ad if so -> What is the error ?

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

              @Pradeep-P-N

              class QPushButton' has no member named 'enabled'
              qDebug() << ui->pushbutton->enabled();
              ^

              Pradeep P NP 1 Reply Last reply
              0
              • ManiRonM ManiRon

                @Pradeep-P-N

                class QPushButton' has no member named 'enabled'
                qDebug() << ui->pushbutton->enabled();
                ^

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

                @ManiRon

                Use qDebug() << ui->pushButton->isEnabled();

                You cannot do ui->pushButton->enabled()

                • Refer enabled.

                Access functions:

                • bool isEnabled() const
                • void setEnabled(bool)

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

                ManiRonM 1 Reply Last reply
                5
                • Pradeep P NP Pradeep P N

                  @ManiRon

                  Use qDebug() << ui->pushButton->isEnabled();

                  You cannot do ui->pushButton->enabled()

                  • Refer enabled.

                  Access functions:

                  • bool isEnabled() const
                  • void setEnabled(bool)
                  ManiRonM Offline
                  ManiRonM Offline
                  ManiRon
                  wrote on last edited by
                  #34

                  @Pradeep-P-N

                  After the button gets enabled the signal automatically called based on my previous click

                  and the print came as

                  Inside Pushbutton
                  ui->Pushbutton->isEnabled() 1
                  Inside Pushbutton
                  ui->Pushbutton->isEnabled() 1
                  Inside Pushbutton
                  ui->Pushbutton->isEnabled() 1

                  Pradeep P NP 1 Reply Last reply
                  0
                  • ManiRonM ManiRon

                    @Pradeep-P-N

                    After the button gets enabled the signal automatically called based on my previous click

                    and the print came as

                    Inside Pushbutton
                    ui->Pushbutton->isEnabled() 1
                    Inside Pushbutton
                    ui->Pushbutton->isEnabled() 1
                    Inside Pushbutton
                    ui->Pushbutton->isEnabled() 1

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

                    @ManiRon You dint debug the code after you disabled right ?

                    • From your log i can clearly see that your PushButton was never disabled.
                        ui->pushButton->setEnabled(false);
                    
                        qDebug() << ui->pushButton->isEnabled();
                    

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

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

                      @ManiRon You dint debug the code after you disabled right ?

                      • From your log i can clearly see that your PushButton was never disabled.
                          ui->pushButton->setEnabled(false);
                      
                          qDebug() << ui->pushButton->isEnabled();
                      
                      ManiRonM Offline
                      ManiRonM Offline
                      ManiRon
                      wrote on last edited by
                      #36

                      @Pradeep-P-N

                      Print was done before disabling
                      Now done after disable
                      Inside Pushbutton
                      ui->Pushbutton->isEnabled() 0
                      Inside Pushbutton
                      ui->Pushbutton->isEnabled() 0

                      Pradeep P NP 1 Reply Last reply
                      0
                      • ManiRonM ManiRon

                        @Pradeep-P-N

                        Print was done before disabling
                        Now done after disable
                        Inside Pushbutton
                        ui->Pushbutton->isEnabled() 0
                        Inside Pushbutton
                        ui->Pushbutton->isEnabled() 0

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

                        Hi @ManiRon

                        • Please don't hurry, and just don't copy paste the code from the posts.
                        • Adding proper debug() messages will help us to understand the code flow and resolve the issue. @jsulm @J-Hilk (Correct me if wrong)
                        • Here the thing is to debug the flow and code.
                        • We should check the flow of the code with respect to issue.
                          • Button is disabled.
                          • Button clicked.
                          • Button gets set to enabled after few seconds.
                          • clicked signal is emitted without button interaction.

                        All the best.

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

                        ManiRonM 1 Reply Last reply
                        2
                        • Pradeep P NP Pradeep P N

                          Hi @ManiRon

                          • Please don't hurry, and just don't copy paste the code from the posts.
                          • Adding proper debug() messages will help us to understand the code flow and resolve the issue. @jsulm @J-Hilk (Correct me if wrong)
                          • Here the thing is to debug the flow and code.
                          • We should check the flow of the code with respect to issue.
                            • Button is disabled.
                            • Button clicked.
                            • Button gets set to enabled after few seconds.
                            • clicked signal is emitted without button interaction.

                          All the best.

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

                          @Pradeep-P-N ya this is the sequence

                          Pradeep P NP 1 Reply Last reply
                          0
                          • ManiRonM ManiRon

                            @Pradeep-P-N ya this is the sequence

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

                            @ManiRon
                            Ok, Please add some debug messages for the flow in your code so we can get it clearly.

                            Just Adding qDebug() << ui->Pushbutton->isEnabled() ; will not provide us the details of the flow to resolve the error.

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

                            1 Reply Last reply
                            1
                            • K Offline
                              K Offline
                              koprok
                              wrote on last edited by
                              #40

                              @ManiRon , @Pradeep-P-N , do you have something new regarding this issue? Did you solve it?
                              I have a similar one, i.e. when clicking on a disabled QPushButton, clicked() signal is emitted once the button gets enabled. My Qt version is 5.9.4 running on Linux.

                              1 Reply Last reply
                              0
                              • D Offline
                                D Offline
                                Daniil 0
                                wrote on last edited by
                                #41

                                Hi all, I am using Qt5.12.3 and have the same problem as @ManiRon and @koprok. (@koprok, read this post carefully you will find the solution to the problem but i think it is still the problem in the library!) For the following code:
                                main.h:

                                #include <QMainWindow>
                                #include <QObject>
                                
                                class QPushButton;
                                class QProgressBar;
                                
                                class mainWindow : public QMainWindow
                                {
                                  Q_OBJECT
                                  
                                  QPushButton* button;
                                  QProgressBar* bar;
                                  
                                public slots:
                                  void on_click();
                                
                                public:
                                  mainWindow();
                                };
                                
                                

                                main.cpp:

                                #include <QApplication>
                                #include <QPushButton>
                                #include <QProgressBar>
                                #include <QStatusBar>
                                #include <QThread>
                                #include <QLayout>
                                #include "main.h"
                                #include <iostream>
                                
                                void mainWindow::on_click()
                                {
                                  button->setEnabled(false);
                                  std::cout << "clicked" << '\n';
                                  for(int i=0;i<100;i++)
                                  {
                                    bar->setValue(i);
                                    QThread::usleep(30000);
                                    //QCoreApplication::processEvents();
                                  }
                                  button->setEnabled(true);
                                }
                                  
                                mainWindow::mainWindow()
                                {
                                  QVBoxLayout* mainLayout = new QVBoxLayout;
                                
                                  button = new QPushButton("Hello world !");
                                  connect(button, &QPushButton::clicked, this, &mainWindow::on_click);
                                  mainLayout->addWidget(button);
                                  bar = new QProgressBar();
                                  bar->setRange(0,100);
                                  bar->setValue(0);
                                  QStatusBar* sb = new QStatusBar();
                                  sb->addWidget(bar);
                                  mainLayout->addWidget(sb);
                                
                                  setCentralWidget(new QWidget);
                                  centralWidget()->setLayout(mainLayout);
                                }
                                
                                int main(int argc, char **argv)
                                {
                                 QApplication app (argc, argv);
                                
                                 mainWindow obj;
                                 obj.show();
                                
                                 return app.exec();
                                }
                                
                                

                                I have the following behavior. When I press a button the progress bar starts to fill up the button becomes inactive. Clicking the button while for loop is not finished does not produce anything (nor progress bar restart nor output to std::cout). But it somehow puts events into a queue and and after loop is finished and button became active it proceeds to next click event which look totally odd.
                                With uncommented processEvents() everything works as expected: while button is inactive (for loop animates the progress) click event does not produce anything and is not stored anywhere, i.e. when button becomes active again nothing happens.
                                Without setEnabled without processEvents it behaves very similar to first variant except user expects something since button look active.
                                Without setEnabled but with processEvents it behaves quite odd from user perspective but still okay since every time active button is started it starts new process and then gets to old one from the point where it was interrupted.
                                I would say only the code presented above as is looks to reveal some real problem in Qt other variants are okay.

                                P.S. Same thing happens for button in QDialog

                                jsulmJ 1 Reply Last reply
                                0
                                • D Daniil 0

                                  Hi all, I am using Qt5.12.3 and have the same problem as @ManiRon and @koprok. (@koprok, read this post carefully you will find the solution to the problem but i think it is still the problem in the library!) For the following code:
                                  main.h:

                                  #include <QMainWindow>
                                  #include <QObject>
                                  
                                  class QPushButton;
                                  class QProgressBar;
                                  
                                  class mainWindow : public QMainWindow
                                  {
                                    Q_OBJECT
                                    
                                    QPushButton* button;
                                    QProgressBar* bar;
                                    
                                  public slots:
                                    void on_click();
                                  
                                  public:
                                    mainWindow();
                                  };
                                  
                                  

                                  main.cpp:

                                  #include <QApplication>
                                  #include <QPushButton>
                                  #include <QProgressBar>
                                  #include <QStatusBar>
                                  #include <QThread>
                                  #include <QLayout>
                                  #include "main.h"
                                  #include <iostream>
                                  
                                  void mainWindow::on_click()
                                  {
                                    button->setEnabled(false);
                                    std::cout << "clicked" << '\n';
                                    for(int i=0;i<100;i++)
                                    {
                                      bar->setValue(i);
                                      QThread::usleep(30000);
                                      //QCoreApplication::processEvents();
                                    }
                                    button->setEnabled(true);
                                  }
                                    
                                  mainWindow::mainWindow()
                                  {
                                    QVBoxLayout* mainLayout = new QVBoxLayout;
                                  
                                    button = new QPushButton("Hello world !");
                                    connect(button, &QPushButton::clicked, this, &mainWindow::on_click);
                                    mainLayout->addWidget(button);
                                    bar = new QProgressBar();
                                    bar->setRange(0,100);
                                    bar->setValue(0);
                                    QStatusBar* sb = new QStatusBar();
                                    sb->addWidget(bar);
                                    mainLayout->addWidget(sb);
                                  
                                    setCentralWidget(new QWidget);
                                    centralWidget()->setLayout(mainLayout);
                                  }
                                  
                                  int main(int argc, char **argv)
                                  {
                                   QApplication app (argc, argv);
                                  
                                   mainWindow obj;
                                   obj.show();
                                  
                                   return app.exec();
                                  }
                                  
                                  

                                  I have the following behavior. When I press a button the progress bar starts to fill up the button becomes inactive. Clicking the button while for loop is not finished does not produce anything (nor progress bar restart nor output to std::cout). But it somehow puts events into a queue and and after loop is finished and button became active it proceeds to next click event which look totally odd.
                                  With uncommented processEvents() everything works as expected: while button is inactive (for loop animates the progress) click event does not produce anything and is not stored anywhere, i.e. when button becomes active again nothing happens.
                                  Without setEnabled without processEvents it behaves very similar to first variant except user expects something since button look active.
                                  Without setEnabled but with processEvents it behaves quite odd from user perspective but still okay since every time active button is started it starts new process and then gets to old one from the point where it was interrupted.
                                  I would say only the code presented above as is looks to reveal some real problem in Qt other variants are okay.

                                  P.S. Same thing happens for button in QDialog

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

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

                                  for(int i=0;i<100;i++)
                                  {
                                  bar->setValue(i);
                                  QThread::usleep(30000);
                                  //QCoreApplication::processEvents();
                                  }

                                  Never do such things in event driven applications! You block Qt event loop! Use a QTimer for updating the progress dialog.

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

                                  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