dcfyg
dcfyg

Reputation: 75

GUI conflict when inheriting from QGraphicsItem+QWidget

I am writing a class which inherits from QGraphicsItem and QWidget. The reason I do this is because I want to make use of setPos() and setRotation() (from QGraphicsItem) to make it draggable and the customContextMenuRequested signal (from QWidget). The code looks like this:

class MyClass: public QWidget, public QGraphicsItem {    
  Q_OBJECT 
/* Variables and Methods here */ 
};

I am not an expert with QT and how it works in every detail. However from testing the code I get the feeling that MyClass actually consists of two GUI elements which may have different positions. The problem is when I put them on top of each other I either cannot get the context menu to work or I am not able to drag the thing. So I assume there can only be one of both at the top (which receives the mouse click event). MyClass is to a QGraphicsScene as a QGraphicsItem and the QGraphicsScene is set to a QGraphicsView, if this is relevant.

Is my assumption correct?

What would be an elegant way to handle this issue?

Thanks in advance!

EDIT: If it helps here is what my application basically should do. User should be able to add multiple MyClass objects and similiar objects to the scene and move/rotate them around freely.

Upvotes: 0

Views: 269

Answers (1)

Developer Paul
Developer Paul

Reputation: 1510

QWidget and QGraphicsItem are fundamentally different things in Qt. From the docs a QGraphicsItem is:

The QGraphicsItem class is the base class for all graphical items in a QGraphicsScene.

It provides a light-weight foundation for writing your own custom items. This includes defining the item's geometry, collision detection, its painting implementation and item interaction through its event handlers. QGraphicsItem is part of the Graphics View Framework

Basically it is only used in a QGraphcisScene.

If you need a custom context menu that is available from a QWidget I would implement this in a custom QGraphicsView which inherits (ultimately) from QWidget. A QGraphicsView can be used to show a QGraphicsScene which will hold your custom QGraphicsItem.

Upvotes: 3

Related Questions