Adding custom widgets to QMenuBar

Valentin H picture Valentin H · Oct 12, 2010 · Viewed 9.5k times · Source

Qt3 used to provide QMenuBar::insertItem with QWidget* parameter. This way any custom widget could be added to menu bar - for example a clock-widget. In Qt4 there is no such overloaded method. What would be the best way to reach the same goal - adding custom widgets to a menu bar? The custom widgets should be integrated in the layout of menu bar. Does anyoune knows the background, why this overload of insertItem was removed in Qt4 API?

Best regards.

Answer

Javier picture Javier · Oct 12, 2010

there's a QMenuBar::addAction ( QAction * action ) method, to add an arbitrary QAction to the menu bar.
For example, it could be a QWidgetAction, which is a subclass of QAction with an associated QWidget instead of just an icon + text.