[SOLVED]Simulating/Generating Button click through QMouseEvent

  • In my device i dont have mouse but I want to generate a button clicked signal by posting MouseEvents through hardware interrupts i get from drivers.
    To simulate this I am using KeyPressEvent

    void MainWindow :: keyPressEvent(QKeyEvent *evntKey)

    if(evntKey->key() == Qt::Key_D)
        QObject*ObjPos = dynamic_cast<QObject*>(QApplication::widgetAt(QCursor::pos()));
        QMouseEvent *event = new QMouseEvent(QEvent::MouseButtonPress,QCursor::pos(), Qt::LeftButton,Qt::LeftButton,Qt::NoModifier);
        QMouseEvent *event1 = new QMouseEvent(QEvent::MouseButtonPress,QCursor::pos(),    Qt::LeftButton,Qt::LeftButton,Qt::NoModifier);


    I am able get mouse pressed and released signals through following code
    Both generating pressed and released signal but not clicked how to do that??? I verified with slots

    void MainWindow::on_pushButton_pressed()
    qDebug()<< "Pressed";

    void MainWindow::on_pushButton_released()
    qDebug()<< "Released";

    [edit: Added missing coding tags @ SGaist]

  • Not totally sure, but I always thought that clicked() was a special signal that had a few criteria that needed to be met.

    1. The pointer is on the widget when the mouse is pressed and released. IE, if only the button's clicked() signal is connected to a slot, and you press on the button then drag the mouse off, the signal will not be emitted.

    2. (Just a hunch) there's a time limit in which you need to let go. Isn't a click supposed to be rather instantaneous?

    For #1, you'd need some type of emulation, but if you're doing this w/o a mouse, For all intents and purposes, I think any time you emit released() you can emit clicked() as well.

  • Sorry for wrong details the pressed/released is not through QMOuseEvent but through the QKeyEvent
    @QKeyEvent *event = new QKeyEvent(QEvent::KeyPress, Qt::Key_Space, Qt::NoModifier, QString(" "));
    QKeyEvent *event1 = new QKeyEvent(QEvent::KeyRelease, Qt::Key_Space, Qt::NoModifier, QString(" "));@

    Am I using QMouseEvent Rightly??

  • Can you paste the whole code block again?

  • gaijinpunch Thanks for your interest.
    I am using @QCursor::pos()@ instead I have to use @QWidget::mapFromGlobal(QCursor::pos())@ to translate it to widget coordinates. Thats it. A small mistake.
    "QPos link here...":http://qt-project.org/doc/qt-4.8/qcursor.html
    This way i am able send clicks only to buttons and line edits if simulation key is NumLock or CapLock I would like to simulate clicks outside QT windows also just like using x11 libraries is it possible

Log in to reply