PDA

View Full Version : QToolBar's extension menu in a QGraphicsScene



francoisjj
5th September 2011, 15:43
I use a QToolBar in a QGraphicsScene and I'm having some trouble with the extension menu.

In the QToolBar documentation it says:


When a QToolBar is not a child of a QMainWindow, it looses the ability to populate the extension pop up with widgets added to the toolbar using addWidget(). Please use widget actions created by inheriting QWidgetAction and implementing QWidgetAction::createWidget() instead.

I use QToolButtons created in QWidgetActions and they are working as expected.

I use the Qt::BypassGraphicsProxyWidget flag in the QToolBar so that the extension menu doesn't get embedded in a QGraphicsProxyWidget when created and displayed. When the QToolBar is resized small enough so that some of the actions in the QToolBar is hidden, the extension button appears.

The problem I'm having is that the extension menu pops up in the wrong position on the screen (far to the left of where the QToolBar is in the QGraphicsScene) and I have no way of positioning it from within my code.

Is there a way to know when the extension menu is about to show or shown and a way to access it? That way I can create and position my own QMenu with the hidden actions in.

Is there an easier way to do this?

Any help is greatly appreciated,
Francois

EDIT: I forgot to mention that when I don't use the Qt::BypassGraphicsProxyWidget flag, the extension menu pops up in the correct position on the graphics scene but underneath other items. An alternative acceptable solution would be if I can somehow force it to the top of the scene's z-order

francoisjj
6th September 2011, 20:28
For anyone that is interested, I managed to solve this in a very hacky way.

I iterate through the toolbar's children and then through the children's children (I went through the source code to see how the extension menu is parented). I then connect their aboutToShow() signal to a slot so that I can access the QMenu through the sender() function. I am then able to move the menu where I want it.

If anyone has a better solution, I am all ears.