PDA

View Full Version : QGraphicsItem and Context Menus



SixDegrees
31st August 2010, 17:45
I'm implementing a QGraphicsItem class. I would like the user to right-click over an object of this class and interact with a context menu, selecting an item that the object will then use to adjust it's iternal state.

Getting the menu to popup is simple, but I'm not sure how to make use of it. I could inherit from QObject and simply implement signals and slots, but there is a potential for a few tens of thousands of these objects to exist at any given time, and QObject is pretty heavy.

The documentation for QGraphicsItem trails off just after menu setup, without actually providing a solution to this problem.

I'll be calling exec() to show the menu synchronously. Should I just examine the menu contents upon return, or use a QActionGroup in a similar fashion? Or is there a better way to do this?

wysota
31st August 2010, 18:36
Menu actions can be connected to slots in the scene and the scene can "call back" to the active item or something like that. You don't need signals and slots in the item in such situation if that's causing you a headache here :)

SixDegrees
31st August 2010, 21:50
I thought about connecting them all to a centralized QObject of some sort. I may still do that, but it looks like all of the possible menu choices will become members of a QActionGroup, which I can examine when the exec() call returns to determine which item was checked. In our case, that's a "cleaner" solution if it will work. Done coding for the day, but I'll do a quick test tomorrow.