How to pass X11 events to QDialog

青春壹個敷衍的年華 提交于 2019-12-07 13:54:37

问题


Currently, I am trying to pass system X11 events (on Linux) to an object I have created. To do this, I have installed an eventFilter onto my object from my QApplication. This works, in that it gets all of the events of the application. However I need to pass the object X11 events as well.

I went ahead and created a x11Event in my object, hoping that it would receive events from X11, but this does not appear to be the case.

Is there anyway to pass X11 events directly to my object, inside of my application?


回答1:


You can receive XEvents through:

  • a filter function set with QAbstractEventDispatcher::instance()->setEventFilter() which will receive all XEvents.
  • a filter function set with qApp->setEventFilter() which will only receive events targeted at the application.
  • a reimplementation of the virtual function QApplication::x11EventFilter
  • a reimplementation of the virtual function QWidget::x11Event for your top level window(s) (child widgets don't receive XEvents).

in that order. If any of these functions returns true for any event, the next function won't receive that event.

Some events can also be filtered by Qt between these functions, for example QWidget::x11Event doesn't receive XKeyEvents (which are filtered by the QInputContext::x11FilterEvent function of the widget which has keyboard focus).

For more details, you should look at Qt sources: QEventDispatcher_x11.cpp and the function QApplication::x11ProcessEvent in QApplication_x11.cpp

So for the most part, if you reimplement only the x11Event function in your QDialog derived class, you should already receive most XEvent. And if you want your child widgets to receive them too, you could call manually their x11Event functions from your reimplementation of QDialog::x11Event.




回答2:


I don't have my dev machine right now so forgive my syntax. I would do the following:

  1. Declare XEvent* as a metatype:

    int main() { qRegisterMetatype<XEvent*>(); }

  2. Reimplement QApplication::x11EventFilter as alexisdm suggested

  3. Create a signal in your QApplication reimplementation for example:

    void dialogEvent(XEvent*);

  4. Than from anywhere in your application you can do the following:

    QApplication *inst = QApllication::instance();

    MyApplication *myApp = qobject_cast<MyApplication*>(inst);

    if(myApp != 0) {

    connect(myApp, SIGNAL(dialogEvent(XEvent*), 
             myDialog, SLOT(onXEvent(XEvent*));
    

    }

This way you can access x11 event globally. As an alternative you can always reimplement:

bool QWidget::x11Event ( XEvent * event )

for individual widgets



来源:https://stackoverflow.com/questions/9041456/how-to-pass-x11-events-to-qdialog

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!