Trouble understanding custom QEvents
-
Hi,
For the last couple of days I have been working on a problem where I want to post a custom event to be handled by custom QGraphicsTextItems. The reason I decided to create a custom event is that I am integrating a custom input device into our product and this seemed consistent with how other input 'events' are handled.
Now, I have managed to get this to work, but not really the way I would like. So I am wondering if I am thinking about this problem the right way or if I just don't understand the way QEvents are used. I am calling,
QApplication::sendEvent(&listener, &event)
where the listener is the QObject that I want receiving the event. This doesn't seem right to me. I expected that I would pass the event to the application or parent and the event would filter down until it is handled. I don't really want to have to register each of my custom QGraphicsTextItems with the source of these events. I want to register the app or scene once, and have it dispatch the events to the appropriate children.
Am I just expecting the Qt event system to do something it was not designed to do? Or have I just failed to find the article that would show me how this is done in Qt?
Like I said, I have a solution to my problem, but I would like to understand the framework better.
-Josh
-
[quote author="jdowner" date="1307980782"]
Am I just expecting the Qt event system to do something it was not designed to do?
[/quote]
I think you are. AFAIK, there is no way to do what you want (I might be wrong here, I never had to use custom events to that extent). You can find a useful discussion of events "here":http://doc.qt.nokia.com/qq/qq11-events.html -
First of all: there's a difference between SENDING an event and POSTING an event. See the aforementioned link for more details.
Note also that input event dispatching in Qt is bottom-up, not top-down. They're sent directly to the "right" widget. If the event is not accepted, event() will return false and QApplication::notify will try sending the event to the parent widget. If you need to do the same for your custom events, subclass QApplication, override notify() and perform the sending going up in the hierarchy.
QGraphicsView introduces another layer of indirection. When an event is posted for a view, it gets translated in a "scene" event, and sent to the scene. The scene, in turn, will dispatch it to the right item by calling its sceneEvent method, which will call the specialized handlers.
Now, if you want to post your events to a QGraphicsItem, just use postEvent(view, event). Subclass QGraphicsView and handle that event in your event() implementation, by converting it in a scene event, and sending it to the scene. Subclass also QGraphicsScene, and in your event() pick the scene event and send it to the right item. Last, process the event in your item.
I hardly suggest you to read the source code of the relevant classes methods, in order to see how it's done for "ordinary" Qt events (mouse, keyboard).