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?
You can receive XEvent
s through:
- a filter function set with
QAbstractEventDispatcher::instance()->setEventFilter()
which will receive all XEvent
s.
- 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 XEvent
s).
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 XKeyEvent
s (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
.
I don't have my dev machine right now so forgive my syntax. I would do the following:
Declare XEvent* as a metatype:
int main() {
qRegisterMetatype<XEvent*>();
}
Reimplement QApplication::x11EventFilter
as alexisdm suggested
Create a signal in your QApplication reimplementation for example:
void dialogEvent(XEvent*);
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