How to use QCoreApplication::postEvent to inject synthetic input events

前端 未结 3 1277
温柔的废话
温柔的废话 2020-12-15 11:52

I\'m injecting Keyboard and Mouse events which are comming over the network into my Qt Application and use QCoreApplication::postEvent for this. The mouse coord

相关标签:
3条回答
  • 2020-12-15 12:15

    I would suggest posting some code as according to the documentation the signature is:

    void QCoreApplication::postEvent ( QObject * receiver, QEvent * event ) [static]
    

    Have you tried giving a pointer to the corresponding QObject as the receiver argument?

    (edit: note that QWidget inherits QObject)

    0 讨论(0)
  • 2020-12-15 12:22

    Here is the Answer that was added to the Question:

    I now use the following which works fine (Many thanks to Dusty Campbell):

    QPoint pos(x, y);
    QWidget *receiver = QApplication::widgetAt(pos);
    if (receiver) {
        QMouseEvent *event = new QMouseEvent(type, receiver->mapFromGlobal(pos),
            mouse_button, mouse_buttons, Qt::NoModifier);
        QCoreApplication::postEvent(receiver, event);
    }
    
    0 讨论(0)
  • 2020-12-15 12:24

    Can you use QApplication::widgetAt() to find the correct widget at the position and then post to that?

    QPoint pos(x, y);
    QMouseEvent *event = new QMouseEvent(type, pos, mouse_button, mouse_buttons,  Qt::NoModifier);
    QWidget *receiver = QApplication::widgetAt(pos);
    QCoreApplication::postEvent(receiver, event);
    

    I wouldn't expect that you would have to do this for the key events though. They should be sent to the focused widget (QApplication::focusWidget()).

    Unfortunately, I haven't tested any of this.

    0 讨论(0)
提交回复
热议问题