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 avoid clicking button twice in Qt
Forum Updated to NodeBB v4.3 + New Features

How to avoid clicking button twice in Qt

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 4 Posters 2.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.
  • L Offline
    L Offline
    LeonSu
    wrote on 18 Jul 2018, 02:53 last edited by LeonSu
    #1

    Hello !

    I want to avoid clicking button twice during sometime.

    I tried some method in the following
    Method 1

    int clicktime = 0;
    void start()
    {
       ui->button->setEnabled(false);
       clicktime++;
       qDebug() << clicktime ;
       QThread::msleep(1000);
       ui->button->setEnabled(true);
    }
    

    when I clicked the button many times quickly, the function will run many times slowly.

    now I try to block the signals of button

    Method 2

    int clicktime = 0;
    void start()
    {
       ui->button->blocksignals(true);
       ui->button->setEnabled(false);
       clicktime++;
       qDebug() << clicktime ;
       QThread::msleep(1000);
       ui->button->setEnabled(true);
       ui->button->blocksignals(false);
    
    }
    

    After I push the button many times, quickly, the function also run many times.
    The result of Method 1 is the same with Method 2

    I tried to disconnected the button from function start.
    It is not work.

    Could any one can help me or get me some suggestions?
    Thanks in advance.

    D 1 Reply Last reply 18 Jul 2018, 03:48
    0
    • L LeonSu
      18 Jul 2018, 02:53

      Hello !

      I want to avoid clicking button twice during sometime.

      I tried some method in the following
      Method 1

      int clicktime = 0;
      void start()
      {
         ui->button->setEnabled(false);
         clicktime++;
         qDebug() << clicktime ;
         QThread::msleep(1000);
         ui->button->setEnabled(true);
      }
      

      when I clicked the button many times quickly, the function will run many times slowly.

      now I try to block the signals of button

      Method 2

      int clicktime = 0;
      void start()
      {
         ui->button->blocksignals(true);
         ui->button->setEnabled(false);
         clicktime++;
         qDebug() << clicktime ;
         QThread::msleep(1000);
         ui->button->setEnabled(true);
         ui->button->blocksignals(false);
      
      }
      

      After I push the button many times, quickly, the function also run many times.
      The result of Method 1 is the same with Method 2

      I tried to disconnected the button from function start.
      It is not work.

      Could any one can help me or get me some suggestions?
      Thanks in advance.

      D Offline
      D Offline
      Devopia53
      wrote on 18 Jul 2018, 03:48 last edited by
      #2

      @LeonSu

      If possible, do not use the sleep () function in the GUI. Thereby causing the GUI to be blocked.

      See the following sample:

      connect(ui->button, &QPushButton::clicked, [&]{
          qDebug() << "clicked";
          ui->button->setEnabled(false);
          QTimer::singleShot(1000, [&]{ ui->button->setEnabled(true); });
      });
      
      
      1 Reply Last reply
      6
      • L Offline
        L Offline
        LeonSu
        wrote on 18 Jul 2018, 08:31 last edited by
        #3

        @Devopia53

        Thank you for your solution.

        Your solution is work well in Qt 5.9.3, but there are some errors in Qt 5.3.2.

        I find another solution is work OK, also

        int clicktime = 0;
        void start()
        {
           ui->button->blocksignals(true);
           ui->button->setEnabled(false);
           clicktime++;
           qDebug() << clicktime ;
           QThread::msleep(1000);
           ui->button->setEnabled(true);
           ui->button->blocksignals(false);
           QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
        }
        

        I think your method is batter.
        I will study how to using your method in Qt 5.3.2

        thanks again.
        LeonSu

        J J D 4 Replies Last reply 18 Jul 2018, 08:40
        0
        • L LeonSu
          18 Jul 2018, 08:31

          @Devopia53

          Thank you for your solution.

          Your solution is work well in Qt 5.9.3, but there are some errors in Qt 5.3.2.

          I find another solution is work OK, also

          int clicktime = 0;
          void start()
          {
             ui->button->blocksignals(true);
             ui->button->setEnabled(false);
             clicktime++;
             qDebug() << clicktime ;
             QThread::msleep(1000);
             ui->button->setEnabled(true);
             ui->button->blocksignals(false);
             QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
          }
          

          I think your method is batter.
          I will study how to using your method in Qt 5.3.2

          thanks again.
          LeonSu

          J Offline
          J Offline
          JonB
          wrote on 18 Jul 2018, 08:40 last edited by
          #4

          @LeonSu said in How to avoid clicking button twice in Qt:

          QThread::msleep(1000);

          Sorry, but anything which involves QThread::msleep(); or ::sleep(), regardless of Qt version, is just wrong/bad/unusable/interferes unacceptably with user interaction.

          1 Reply Last reply
          5
          • L LeonSu
            18 Jul 2018, 08:31

            @Devopia53

            Thank you for your solution.

            Your solution is work well in Qt 5.9.3, but there are some errors in Qt 5.3.2.

            I find another solution is work OK, also

            int clicktime = 0;
            void start()
            {
               ui->button->blocksignals(true);
               ui->button->setEnabled(false);
               clicktime++;
               qDebug() << clicktime ;
               QThread::msleep(1000);
               ui->button->setEnabled(true);
               ui->button->blocksignals(false);
               QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
            }
            

            I think your method is batter.
            I will study how to using your method in Qt 5.3.2

            thanks again.
            LeonSu

            J Offline
            J Offline
            jsulm
            Lifetime Qt Champion
            wrote on 18 Jul 2018, 08:46 last edited by
            #5

            @LeonSu said in How to avoid clicking button twice in Qt:

            I will study how to using your method in Qt 5.3.2

            Just don't use lambda, use normal slot in older Qt versions instead...

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

            1 Reply Last reply
            4
            • L LeonSu
              18 Jul 2018, 08:31

              @Devopia53

              Thank you for your solution.

              Your solution is work well in Qt 5.9.3, but there are some errors in Qt 5.3.2.

              I find another solution is work OK, also

              int clicktime = 0;
              void start()
              {
                 ui->button->blocksignals(true);
                 ui->button->setEnabled(false);
                 clicktime++;
                 qDebug() << clicktime ;
                 QThread::msleep(1000);
                 ui->button->setEnabled(true);
                 ui->button->blocksignals(false);
                 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
              }
              

              I think your method is batter.
              I will study how to using your method in Qt 5.3.2

              thanks again.
              LeonSu

              J Offline
              J Offline
              JonB
              wrote on 18 Jul 2018, 08:55 last edited by JonB
              #6

              @LeonSu
              BTW, what is so special about preventing another click within 1 second? Why not half a second or 2 seconds or whatever? Is your actual intention to prevent a second click while the code for the first click is still executing? In which case, your re-enablement of the button should not be on some kind of timer but rather when the first click handler's code has completed?

              1 Reply Last reply
              3
              • L LeonSu
                18 Jul 2018, 08:31

                @Devopia53

                Thank you for your solution.

                Your solution is work well in Qt 5.9.3, but there are some errors in Qt 5.3.2.

                I find another solution is work OK, also

                int clicktime = 0;
                void start()
                {
                   ui->button->blocksignals(true);
                   ui->button->setEnabled(false);
                   clicktime++;
                   qDebug() << clicktime ;
                   QThread::msleep(1000);
                   ui->button->setEnabled(true);
                   ui->button->blocksignals(false);
                   QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
                }
                

                I think your method is batter.
                I will study how to using your method in Qt 5.3.2

                thanks again.
                LeonSu

                D Offline
                D Offline
                Devopia53
                wrote on 19 Jul 2018, 01:26 last edited by
                #7

                @LeonSu

                Your solution is work well in Qt 5.9.3, but there are some errors in Qt 5.3.2.

                Qt 5.3.2 supports C++11. If the compiler supports it, add the following directive in your .pro file. I tested it with the Qt 5.3.2 / MinGW 4.8.2 version.

                like this: CONFIG += c++11

                1 Reply Last reply
                1
                • L Offline
                  L Offline
                  LeonSu
                  wrote on 19 Jul 2018, 01:47 last edited by LeonSu
                  #8

                  @jsulm
                  lambda can be compiled with C++11.
                  but I get the error message : "no matching function for call to 'QTimer::singleShot(int, MainWindow::bigbuttontest()<lambda()>'.....

                  The Qt 5.3.2 does not support this method.
                  There are only two overloaded functions.

                  /
                   void	singleShot(int msec, const QObject * receiver, const char * member)
                   void      singleShot(int msec, Qt::TimerType timerType, const QObject * receiver, const char * member)
                  

                  In Qt 5.9.3 had ten overloaded functions

                  void  singleShot(int msec, const QObject *receiver, const char *member)
                  void  singleShot(int msec, Qt::TimerType timerType, const QObject *receiver, const char *member)
                  void  singleShot(int msec, const QObject *receiver, PointerToMemberFunction method)
                  void  singleShot(int msec, Qt::TimerType timerType, const QObject *receiver, PointerToMemberFunction method)
                  void singleShot(int msec, Functor functor)
                  void singleShot(int msec, Qt::TimerType timerType, Functor functor)
                  void singleShot(int msec, const QObject *context, Functor functor)
                  void singleShot(int msec, Qt::TimerType timerType, const QObject *context, Functor functor)
                  void singleShot(std::chrono::milliseconds msec, const QObject *receiver, const char *member)
                  void singleShot(std::chrono::milliseconds msec, Qt::TimerType timerType, const QObject *receiver, const char *member)
                  
                  

                  @JonB
                  I try to avoid to clicking button twice during 1 second.

                  @Devopia53
                  Qt 5.3.2 does not support this method.

                  D 1 Reply Last reply 19 Jul 2018, 06:21
                  1
                  • L LeonSu
                    19 Jul 2018, 01:47

                    @jsulm
                    lambda can be compiled with C++11.
                    but I get the error message : "no matching function for call to 'QTimer::singleShot(int, MainWindow::bigbuttontest()<lambda()>'.....

                    The Qt 5.3.2 does not support this method.
                    There are only two overloaded functions.

                    /
                     void	singleShot(int msec, const QObject * receiver, const char * member)
                     void      singleShot(int msec, Qt::TimerType timerType, const QObject * receiver, const char * member)
                    

                    In Qt 5.9.3 had ten overloaded functions

                    void  singleShot(int msec, const QObject *receiver, const char *member)
                    void  singleShot(int msec, Qt::TimerType timerType, const QObject *receiver, const char *member)
                    void  singleShot(int msec, const QObject *receiver, PointerToMemberFunction method)
                    void  singleShot(int msec, Qt::TimerType timerType, const QObject *receiver, PointerToMemberFunction method)
                    void singleShot(int msec, Functor functor)
                    void singleShot(int msec, Qt::TimerType timerType, Functor functor)
                    void singleShot(int msec, const QObject *context, Functor functor)
                    void singleShot(int msec, Qt::TimerType timerType, const QObject *context, Functor functor)
                    void singleShot(std::chrono::milliseconds msec, const QObject *receiver, const char *member)
                    void singleShot(std::chrono::milliseconds msec, Qt::TimerType timerType, const QObject *receiver, const char *member)
                    
                    

                    @JonB
                    I try to avoid to clicking button twice during 1 second.

                    @Devopia53
                    Qt 5.3.2 does not support this method.

                    D Offline
                    D Offline
                    Devopia53
                    wrote on 19 Jul 2018, 06:21 last edited by
                    #9

                    @LeonSu

                    As suggested by @jsulm, use an alternative string-based SLOT () macro.

                    1 Reply Last reply
                    0

                    1/9

                    18 Jul 2018, 02:53

                    • Login

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