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

Click and longClick in the same button

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 1.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.
  • ivanicyI Offline
    ivanicyI Offline
    ivanicy
    wrote on 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!

    Gojir4G 1 Reply Last reply
    0
    • ivanicyI ivanicy

      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!

      Gojir4G Offline
      Gojir4G Offline
      Gojir4
      wrote on 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;
      }
      
      ivanicyI 1 Reply Last reply
      2
      • Gojir4G Gojir4

        @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;
        }
        
        ivanicyI Offline
        ivanicyI Offline
        ivanicy
        wrote on 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!

        Pl45m4P Gojir4G 3 Replies Last reply
        0
        • ivanicyI ivanicy

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

          Pl45m4P Offline
          Pl45m4P Offline
          Pl45m4
          wrote on last edited by
          #4
          This post is deleted!
          1 Reply Last reply
          0
          • ivanicyI ivanicy

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

            Gojir4G Offline
            Gojir4G Offline
            Gojir4
            wrote on 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
            • ivanicyI ivanicy

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

              Pl45m4P Offline
              Pl45m4P Offline
              Pl45m4
              wrote on 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
              • ivanicyI Offline
                ivanicyI Offline
                ivanicy
                wrote on last edited by
                #7

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

                Gojir4G 1 Reply Last reply
                1
                • ivanicyI ivanicy

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

                  Gojir4G Offline
                  Gojir4G Offline
                  Gojir4
                  wrote on last edited by
                  #8

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

                  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