How to pass X11 events to QDialog

2019-05-16 22:28发布

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?

标签: c++ linux qt qt4
2条回答
Viruses.
2楼-- · 2019-05-16 23:10

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.

查看更多
趁早两清
3楼-- · 2019-05-16 23:23

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

查看更多
登录 后发表回答