Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED]Simulating/Generating Button click through QMouseEvent
Forum Update on Monday, May 27th 2025

[SOLVED]Simulating/Generating Button click through QMouseEvent

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 5.7k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    vkdinar
    wrote on last edited by
    #1

    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);
        QCoreApplication::sendEvent(ObjPos,event);
        QMouseEvent *event1 = new QMouseEvent(QEvent::MouseButtonPress,QCursor::pos(),    Qt::LeftButton,Qt::LeftButton,Qt::NoModifier);
        QCoreApplication::sendEvent(ObjPos,event1);
    

    }
    @

    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]

    1 Reply Last reply
    0
    • G Offline
      G Offline
      gaijinpunch
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • V Offline
        V Offline
        vkdinar
        wrote on last edited by
        #3

        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??

        1 Reply Last reply
        0
        • G Offline
          G Offline
          gaijinpunch
          wrote on last edited by
          #4

          Can you paste the whole code block again?

          1 Reply Last reply
          0
          • V Offline
            V Offline
            vkdinar
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved