Qt World Summit: Submit your Presentation

QPushButton down signal

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

  • One of the solution:
    @void MainWindow::on_pushButton_pressed()
    int i = 0;
    while (pushButton->isDown()) {

  • 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.

  • 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.

  • 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() {
    QTimer::singleShot(200, this, SLOT(myFunction()));

    Thank you so much for your time!

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

  • 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 :-) )

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

  • 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.

  • This code works for me:

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




    #include <QDialog>

    class QTimer;

    namespace Ui {
    class ButtonIncrementer;

    class ButtonIncrementer : public QDialog

    explicit ButtonIncrementer(QWidget *parent = 0);


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

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




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

    ButtonIncrementer::ButtonIncrementer(QWidget *parent) :
    ui(new Ui::ButtonIncrementer)
    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()));

    delete ui;

    void ButtonIncrementer::buttonPressed()
    timerTimeout = 2000;

    void ButtonIncrementer::buttonReleased()

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

  • Thank you! Works like a charm ;)

Log in to reply