c++qtqgraphicsviewqpushbuttontranslate-animation

Execute operation periodically while QPushButotn is pressed


I've a QGraphicsView in which I've some button for translate the view. When I click on one of those button the view is translated. I use the following connection for performing the operation (I'm subclassing QGraphicsView:

connect(m_upButton, &QPushButton::clicked, [this](){ verticalScrollBar()->setValue( verticalScrollBar()->value() - TranslateFactor ); });
connect(m_downButton, &QPushButton::clicked, [this](){ verticalScrollBar()->setValue( verticalScrollBar()->value() + TranslateFactor ); });
connect(m_leftButton, &QPushButton::clicked, [this](){ horizontalScrollBar()->setValue( horizontalScrollBar()->value() - TranslateFactor ); });
connect(m_rightButton, &QPushButton::clicked, [this](){ horizontalScrollBar()->setValue( horizontalScrollBar()->value() + TranslateFactor ); });

It works, but I want also that until one of those button is pressed, I translate periodically the image, so the user can hold down the mouse on the corresponding button for translating the view of the desired amount, instead of clicking many times. How can I obtain this result?


Solution

  • QAbstractButton has a property called autoRepeat which does what you want.

    Do not forget to look at autoRepeatDelay and autoRepeatInterval too.

    Check the doc for more information.

    https://doc.qt.io/qt-5/qabstractbutton.html