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. Click and longClick in the same button
Forum Updated to NodeBB v4.3 + New Features

Click and longClick in the same button

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 1.9k Views 2 Watching
  • 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.
  • I Offline
    I Offline
    ivanicy
    wrote on 24 Sept 2019, 07:54 last edited by ivanicy
    #1

    Hello! I have a QPushButton which has three slots, clicked, pressed and released. This is my code:

    void MainWindow::on_button_clicked()
    {
        qDebug() << "Click pressed";
    }
    
    void MainWindow::on_button_pressed()
    {
        m_pTimer->start(2000);
    }
    
    void MainWindow::on_button_released()
    {
        m_pTimer->stop();
    }
    
    void MainWindow::longClick() {
        qDebug() << "Long click pressed";
    }
    

    Well, I want that, when the timer has a timeout (longClick()), ignore de click event. How can I do it? Thank you very much!

    G 1 Reply Last reply 24 Sept 2019, 08:18
    0
    • I ivanicy
      24 Sept 2019, 07:54

      Hello! I have a QPushButton which has three slots, clicked, pressed and released. This is my code:

      void MainWindow::on_button_clicked()
      {
          qDebug() << "Click pressed";
      }
      
      void MainWindow::on_button_pressed()
      {
          m_pTimer->start(2000);
      }
      
      void MainWindow::on_button_released()
      {
          m_pTimer->stop();
      }
      
      void MainWindow::longClick() {
          qDebug() << "Long click pressed";
      }
      

      Well, I want that, when the timer has a timeout (longClick()), ignore de click event. How can I do it? Thank you very much!

      G Offline
      G Offline
      Gojir4
      wrote on 24 Sept 2019, 08:18 last edited by
      #2

      @ivanicy Hello,

      Here is a possible solution:

      //widget.h
      quint64 m_pressedTime = 0;
      
      //widget.cpp
      Widget::Widget(QWidget *parent) :
          QWidget(parent),
          ui(new Ui::Widget)
      {
          ui->setupUi(this);
      
          connect(ui->pushButton, &QPushButton::pressed, [&]{ m_pressedTime = QDateTime::currentMSecsSinceEpoch(); });
          connect(ui->pushButton, &QPushButton::released, this, &Widget::released);
      }
      
      void Widget::released()
      {
          quint64 now = QDateTime::currentMSecsSinceEpoch();
          if(now - m_pressedTime > 2000){
              qDebug() << "long clicked";
          } else {
              qDebug() << "clicked";
          }
          m_pressedTime = 0;
      }
      
      I 1 Reply Last reply 24 Sept 2019, 08:34
      2
      • G Gojir4
        24 Sept 2019, 08:18

        @ivanicy Hello,

        Here is a possible solution:

        //widget.h
        quint64 m_pressedTime = 0;
        
        //widget.cpp
        Widget::Widget(QWidget *parent) :
            QWidget(parent),
            ui(new Ui::Widget)
        {
            ui->setupUi(this);
        
            connect(ui->pushButton, &QPushButton::pressed, [&]{ m_pressedTime = QDateTime::currentMSecsSinceEpoch(); });
            connect(ui->pushButton, &QPushButton::released, this, &Widget::released);
        }
        
        void Widget::released()
        {
            quint64 now = QDateTime::currentMSecsSinceEpoch();
            if(now - m_pressedTime > 2000){
                qDebug() << "long clicked";
            } else {
                qDebug() << "clicked";
            }
            m_pressedTime = 0;
        }
        
        I Offline
        I Offline
        ivanicy
        wrote on 24 Sept 2019, 08:34 last edited by
        #3

        @Gojir4 That works fine but, I need a small improve. I need that when this two seconds pass, do the action, not on the release event.

        Thank you for your time!

        P G 3 Replies Last reply 24 Sept 2019, 09:37
        0
        • I ivanicy
          24 Sept 2019, 08:34

          @Gojir4 That works fine but, I need a small improve. I need that when this two seconds pass, do the action, not on the release event.

          Thank you for your time!

          P Offline
          P Offline
          Pl45m4
          wrote on 24 Sept 2019, 09:37 last edited by
          #4
          This post is deleted!
          1 Reply Last reply
          0
          • I ivanicy
            24 Sept 2019, 08:34

            @Gojir4 That works fine but, I need a small improve. I need that when this two seconds pass, do the action, not on the release event.

            Thank you for your time!

            G Offline
            G Offline
            Gojir4
            wrote on 24 Sept 2019, 09:45 last edited by
            #5

            @ivanicy I knew I should have kept the timer...

            Updated using timer

            //widget.h
                QTimer m_longClickTimer;
            
            //widget.cpp
            Widget::Widget(QWidget *parent) :
                QWidget(parent),
                ui(new Ui::Widget)
            {
                ui->setupUi(this);
            
                m_longClickTimer.setSingleShot(true);
                connect(ui->pushButton, &QPushButton::pressed, [&]{ m_longClickTimer.start(2000); });
                connect(ui->pushButton, &QPushButton::released, this, &Widget::released);
                connect(&m_longClickTimer, &QTimer::timeout, this, &Widget::longClickTimeout);
            }
            void Widget::released()
            {
                if(m_longClickTimer.isActive()){
                    qDebug() << "clicked";
                    m_longClickTimer.stop();
                }
            }
            
            void Widget::longClickTimeout()
            {
                qDebug() << "long clicked";
            }
            
            1 Reply Last reply
            2
            • I ivanicy
              24 Sept 2019, 08:34

              @Gojir4 That works fine but, I need a small improve. I need that when this two seconds pass, do the action, not on the release event.

              Thank you for your time!

              P Offline
              P Offline
              Pl45m4
              wrote on 24 Sept 2019, 09:45 last edited by
              #6

              @ivanicy

              Start a single shot timer with a 2 sec timeout, every time the button was pressed. If timer fires at 2s and button still pressed, start your action, else reset the time (on release also reset your time).

              1 Reply Last reply
              2
              • I Offline
                I Offline
                ivanicy
                wrote on 24 Sept 2019, 14:34 last edited by
                #7

                @Gojir4 @Pl45m4 Thank you very much both of you!

                G 1 Reply Last reply 24 Sept 2019, 14:49
                1
                • I ivanicy
                  24 Sept 2019, 14:34

                  @Gojir4 @Pl45m4 Thank you very much both of you!

                  G Offline
                  G Offline
                  Gojir4
                  wrote on 24 Sept 2019, 14:49 last edited by
                  #8

                  @ivanicy Glad it helped. Please don't forget to set the post as solved !

                  1 Reply Last reply
                  0

                  1/8

                  24 Sept 2019, 07:54

                  • Login

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