Reputation: 414
I have crossplatform Qt application with custom graphics scene inherited from QGraphicsScene and QGraphicsItem-based elements on it. Both my scene and custom items have context menu so I overload contextMenuEvent() method in my classes. Also I overload all the others mouse event handlers (mousePressEvent(), mouseReleaseEvent(), mouseDoubleClickEvent() and so on). I want to have the same behaviour of my application on different operating systems. On Windows contextMenuEvent() handler is called after mouseReleaseEvent() handler, but on unix systems first mouseReleaseEvent() handler called and only after that contextMenuEvent() handler. When I perform right mouse button click on the item on scene context menu of item is shown and MyGraphicsScene::focusOutEvent() is called. So event handlers sequence on Windows is:
MyGraphicsScene::mousePressEvent()
MyGraphicsItem::mousePressEvent()
MyGraphicsScene::mouseReleaseEvent()
MyGraphicsItem::mouseReleaseEvent()
MyGraphicsScene::contextMenuEvent()
MyGraphicsItem::contextMenuEvent()
MyGraphicsScene::focusOutEvent()
Event handlers sequence on Linux is:
MyGraphicsScene::mousePressEvent()
MyGraphicsItem::mousePressEvent()
MyGraphicsScene::contextMenuEvent()
MyGraphicsItem::contextMenuEvent()
MyGraphicsScene::focusOutEvent()
and mouseReleaseEvent() handler is not called.
I perform very essential actions in mouseReleaseEvent() methods and have to make some workarounds on Linux or perform additional actions to have the proper behaviour. As Qt documantation states mouseReleaseEvent() handler clears mouse grabber item of the scene so as I understand loosing of mouseReleaseEvent() call cause situation that previous mouse grabber item stay the same and can still get all the events from the scene.
To be fully clear on Linux I get the situation in my app when I call items context menu with right mouse button, click on menu to perform some action with item (e.g. rotate it) and after that when I want to move the item it can not be moved at first time - I have to perform click on scene and only after that I can move the item. On Windows there is no such problems as menu item is shown in contextMenuEvent() handler which is called after mouseReleaseEvent().
I use Qt 4.6 version in my application.
Do someone deal with such problem before? How can I make the same proper events handling on Linux as it is on Windows?
Upvotes: 1
Views: 339
Reputation: 71
You can use the preprocessor. Your compiler should have some predefined macros to identify which OS your code is being compiled for.
#ifdef __linux__
// Linux event loop
#endif
#ifdef _WIN32
// Windows event loop
#endif
Upvotes: 1