PDA

View Full Version : Simulating a Button Click signal



Ratheendrans
5th May 2010, 10:34
Hi All,

I am trying to simulate a button click signal on an external event (presently I trying with Keyboard)

I tried sending an mouse event on the focused widget by the code given below. This code is not able to call the corresponding slot initialized in the constructor.

QMouseEvent mouseEvent(QEvent::MouseButtonPress,QCursor :: Pos(),Qt::LeftButton,Qt::LeftButton,Qt::NoModifier );
QApplication::sendEvent(this->focusWidget(), &mouseEvent);

And also I am not able to call mouse click simulation using QTest methods.
QTest::mouseClick(this->focusWidget(),Qt::LeftButton,Qt::NoModifier,QCurso r ::Pos());



Kindly give your valuable suggestion and help me in fixing this issue.

Thanks in Advance.

Ratheendran

aamer4yu
5th May 2010, 10:45
Shouldn't you send event to qApp->focusWidget() ? :rolleyes:

Or you may also use QAbstractButton::click ()

Ratheendrans
5th May 2010, 11:00
Thanks for your response.

I relatively new to QT, I am not able to comprehend your reply.

can you give me more details/ example of simple implementation.

Ratheendran

aamer4yu
5th May 2010, 11:37
QApplication::sendEvent(this->focusWidget(), &mouseEvent);
should be
QApplication::sendEvent(qApp->focusWidget(), &mouseEvent);

and the place where you are calling this code from, you can instead use button->click(); where button is the pointer to the button you want to simulate the click for.

Ratheendrans
5th May 2010, 11:57
Thanks again,

I tried "QApplication::sendEvent(qApp->focusWidget(), &mouseEvent);" in my code this is not working.

My application required signal to be send to the button on which cursor is active.

so can you suggest any other alternative.

Thanks,
Ratheendran