PDA

View Full Version : Emitting a series of signals as long as a QPushButton is pressed



qbird
17th November 2009, 13:47
Hello,

I'd like to move a cursor in a function plot (Qwt-Plot) using two QPushButtons (left, right) to control the movement. The cursor shall move in one direction as long as the corresponding button is pressed.

If I connect the movement of the cursor to the SIGNAL(clicked()) of the QPushButtons, the cursor moves only one step forward each time the button is clicked. The user has to click the button many times to move the cursor over a longer distance. That's not what I want. I want the cursor to keep moving as long as the QPushButton is hold down. Somehow the QPushButton should keep firing signals as long as it is pressed.

Meanwhile I think I have to reimplement the QPushButton::mousePressEvent method. But as a novice to Qt I'm not sure whether this is the right way to go. Therefore any hints or suggestions are very welcome.

Thanks,
Dirk

Lykurg
17th November 2009, 14:21
Meanwhile I think I have to reimplement the QPushButton::mousePressEvent method. But as a novice to Qt I'm not sure whether this is the right way to go. Therefore any hints or suggestions are very welcome.

I am sorry, but this is the way you have to take. But it isn't such difficult. Reimp mousePressEvent and start a timer firing signals, and stop with mouseReleaseEvent. Or better, only emit a "pressStart" signal and do the continuous moving inside the slot and stop it when you receive a "pressStop" signal. That would be better then firing signals all the time.

Coises
17th November 2009, 15:52
QPushButton derives from QAbstractButton; perhaps the auto-repeat property (QAbstractButton::autoRepeat-prop) will give you what you need?

qbird
17th November 2009, 21:10
Hi Lykurg and Coises, thanks for your quick replies.

Special thanks to you, Coises ... this was the hint I was looking for. Two simple lines of code


btnMoveCursorLeft->setAutoRepeat(true);
btnMoveCursorRight->setAutoRepeat(true);


have solved my problem :D

Great forum, indeed !