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. QPushButton down signal

QPushButton down signal

Scheduled Pinned Locked Moved General and Desktop
12 Posts 4 Posters 10.1k Views 1 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.
  • M Offline
    M Offline
    maxmotor
    wrote on last edited by
    #1

    Hello Qt devs!

    I'm trying to make an application, which should enable the user to increment or decrement a variable by using two QPushButtons called "up" and "down". When the user presses the "up" button, the variable is incremented like intended. I would like to add a feature where if the user holds down the "up" button, then the variable will continue increasing. The longer the user holds down the button, the greater the incrementation steps should be.

    But I guess my main question is: what signal from QPushButton is sent when a button is held down?

    Thank you for your time!

    1 Reply Last reply
    0
    • R Offline
      R Offline
      RazrFalcon
      wrote on last edited by
      #2

      One of the solution:
      @void MainWindow::on_pushButton_pressed()
      {
      int i = 0;
      while (pushButton->isDown()) {
      qDebug()<<i++;
      QApplication::processEvents();
      }
      }@

      1 Reply Last reply
      0
      • G Offline
        G Offline
        giesbert
        wrote on last edited by
        #3

        A button has several signals, e.g.:

        • "pressed":http://doc.qt.nokia.com/4.7/qabstractbutton.html#pressed
        • "release":http://doc.qt.nokia.com/4.7/qabstractbutton.html#released
        • ...

        Nokia Certified Qt Specialist.
        Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

        1 Reply Last reply
        0
        • G Offline
          G Offline
          giesbert
          wrote on last edited by
          #4

          you will not get additional events for the pressed state. you could do that via a timer and in the timer slot check, whether the button is still pressed.

          Nokia Certified Qt Specialist.
          Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

          1 Reply Last reply
          0
          • G Offline
            G Offline
            goetz
            wrote on last edited by
            #5

            A little more details on Gerolf's suggestion:
            Connect the pressed signal to a slot, do the increment, start a timer with a longer timeout, when the timer fires do another increment, restart the timer with a shorter timeout and redo that. Connect the release signal to the stop slot of the timer.

            http://www.catb.org/~esr/faqs/smart-questions.html

            1 Reply Last reply
            0
            • M Offline
              M Offline
              maxmotor
              wrote on last edited by
              #6

              Thank you so much for your help!

              A special thanks to Volker for spelling it out to me! Dunno if that's the correct saying :)

              This is how it looks - in a simplified version:

              @void MyClass::myFunction() {
              number++;
              QTimer::singleShot(200, this, SLOT(myFunction()));
              }@

              Thank you so much for your time!

              1 Reply Last reply
              0
              • M Offline
                M Offline
                maxmotor
                wrote on last edited by
                #7

                Okay okay I forgot the other half - stopping the slot again. I'll work on that ;)

                1 Reply Last reply
                0
                • G Offline
                  G Offline
                  goetz
                  wrote on last edited by
                  #8

                  That's it bascially, yes.

                  To accelerate the increment, you should decrease the timer's timeout from iteration to iteration (e.g. by dividing the previous timeout by 2 or subtracting a fixed time - don't forget to stop at a sane lower bound, though :-) )

                  http://www.catb.org/~esr/faqs/smart-questions.html

                  1 Reply Last reply
                  0
                  • G Offline
                    G Offline
                    goetz
                    wrote on last edited by
                    #9

                    For the second part, you need to have the timer as a member in your class. It doesn't work with the static method.

                    http://www.catb.org/~esr/faqs/smart-questions.html

                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      maxmotor
                      wrote on last edited by
                      #10

                      For some reason I cant figure out, the variable is doubled at every timeout.

                      When releasing instantly, the number is incremented with one - as expected.

                      1 Reply Last reply
                      0
                      • G Offline
                        G Offline
                        goetz
                        wrote on last edited by
                        #11

                        This code works for me:

                        The ui contains a QPushButton (pushButton) and a QLabel (label), you can easily create it with Qt Designer.

                        buttonincrementer.h

                        @

                        #ifndef BUTTONINCREMENTER_H
                        #define BUTTONINCREMENTER_H

                        #include <QDialog>

                        class QTimer;

                        namespace Ui {
                        class ButtonIncrementer;
                        }

                        class ButtonIncrementer : public QDialog
                        {
                        Q_OBJECT

                        public:
                        explicit ButtonIncrementer(QWidget *parent = 0);
                        ~ButtonIncrementer();

                        protected:

                        protected slots:
                        void buttonPressed();
                        void buttonReleased();
                        void doIncrement();

                        private:
                        Ui::ButtonIncrementer *ui;
                        QTimer *timer;
                        int timerTimeout;
                        int number;
                        };

                        #endif // BUTTONINCREMENTER_H
                        @

                        buttonincrementer.cpp

                        @

                        #include "buttonincrementer.h"
                        #include "ui_buttonincrementer.h"
                        #include <QTimer>

                        ButtonIncrementer::ButtonIncrementer(QWidget *parent) :
                        QDialog(parent),
                        ui(new Ui::ButtonIncrementer)
                        {
                        ui->setupUi(this);
                        number = 0;
                        timerTimeout = 0;
                        timer = new QTimer(this);
                        connect(timer, SIGNAL(timeout()), this, SLOT(doIncrement()));
                        connect(ui->pushButton, SIGNAL(pressed()), this, SLOT(buttonPressed()));
                        connect(ui->pushButton, SIGNAL(released()), this, SLOT(buttonReleased()));
                        }

                        ButtonIncrementer::~ButtonIncrementer()
                        {
                        delete ui;
                        }

                        void ButtonIncrementer::buttonPressed()
                        {
                        timerTimeout = 2000;
                        doIncrement();
                        }

                        void ButtonIncrementer::buttonReleased()
                        {
                        timer->stop();
                        }

                        void ButtonIncrementer::doIncrement()
                        {
                        ++number;
                        ui->label->setText(QString("Value: %1").arg(number));
                        if(timerTimeout > 50)
                        timerTimeout = timerTimeout / 2;
                        timer->start(timerTimeout);
                        }
                        @

                        http://www.catb.org/~esr/faqs/smart-questions.html

                        1 Reply Last reply
                        0
                        • M Offline
                          M Offline
                          maxmotor
                          wrote on last edited by
                          #12

                          Thank you! Works like a charm ;)

                          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