Qt Designer: develop custom widget plugin getting mouse events


If you want to develop a custom Qt widget to distrbute or sell a good habits is always to develop, in addition, a plugin working into Qt Designer allowing to visually use and configure your custom widget inside the designer GUI editor. Develop such plugin is not a difficult task, official documentation and examples are here and, as usual for Qt, all is well explained.


Basically there are two type of widget plugin, normal and container. Container widget plugin differ from normal since can manage multiple pages in the same area. The classic example of this type of widget is the QTabWidget. Using this particular widget you can add pages and move between pages by simply click on the corresponding tab and it work in the same way both in the final application that in the Qt Designer during GUI creation. Obviously a container widget plugin require a more complex structure but, since the documentation is very good, is not so difficult to develop. However, during the first tests of your plugin, you'll probably note that, inside the Qt Designer editor, the controls of your widget allowing to manage pages selection doesn't work. More precisely it seem it doesn't receive the down and up mouse events with the result you can not move between pages of your widget. The Qt Designer apply some mouse event filter and if you try to double-click over your control the window for set the widget name will be showed instead. The trick for have the widget control managing pages working in "normal" mode (as QTabWidget do) is quite strange and not documented (at least, I didn't find any documentation about). Basically you need to set a name to all the objects of your widget you want to have "mouse interactive" inside the Qt Designer using the special prefix in red:

pMyConrolObject->setObjectName("__qt__passive_ObjectName");

All the object of your widget named with this special convention will be managed by Qt Designer in a special way and will receive all the mouse events. This will allow to have it working as expected also inside the Qt Designer during GUI creation phase.

1 Comments:

Perfect article, thanks!!!

April 23, 2016 at 6:37 PM comment-delete

Post a Comment