Multiple Inheritance from QGraphicsObject and concrete QGraphics*item.
-
I have the following project: I am creating a node editor.
In the first picture there are 2 types of objects, the nodes themselves, and a black stick that connects them - an edge.
The node class looks like this:class Node : public QGraphicsObject
I really need the meta information that is given from the
QObject
class. And as I understood, if I want to attach slots, I will still need to inherit from theQObject
class.The Problem itself is in my EDGE, for the correct operation and calculations of this edge, I use inheritance from the
QGraphicsPathItem
class, but I also need meta information, so we get duplication in inheritance:class NodeEdge : public QGraphicsObject, public QGraphicsPathItem
I found the following guide on the Internet and redefined all virtual methods, as in that guide.
This is how it looks now:
Node Edge.hpp
NodeEdge.cppThe bug is: when I click on an edge, it should be selected and the color changed, so this happens only in the area positive in x and y (pic. 2), if at least one coordinate is negative, then the program behaves as if the object does not exist, but at the same time it is drawn properly.
In the second picture, the plus sign marks the area where the edge behaves normally, in accordance with the idea. -
Since a QGraphicsItem can have children I don't see why it should be needed to derive from QGraphicsObject and QGraphicsPathItem.
-
@Valderman said in Multiple Inheritance from QGraphicsObject and concrete QGraphics*item.:
Do you mean that I need to create a QGraphicsObject object and pass an empty QGraphicsPathItem in the constructor, and then in my NodeEdge class call childrenBoundingRect() in the redefined boundingRect() method?
Since I don't know what you're trying to do - maybe.
You can group QGraphicsItems and it looks like this is what you want to do. -
@Christian-Ehrlicher said in Multiple Inheritance from QGraphicsObject and concrete QGraphics*item.:
Since I don't know what you're trying to do - maybe.
In a nutshell, I want to create an object with the properties of
QGraphicsObject
andQGraphicsPathItem
. In the future, I will interact with this object asQGraphicsObject
, get its name, position and other meta-information. And at the same time it should be drawn asQGraphicsPathItem
withsetPath()
orpath()
.This is where I got double inheritance and errors.
Btw my code with this class is attached in the links to pastebin in first post. -
Then use QGraphicsPathItem as a child item and pass all the stuff you need through your QGraphicsObject.
-
To be honest, I couldn't do it with your method. But after writing the following line
QGraphicsObject::setPos({-0.0001, -0.0001});
in
NodeEdge::paint(QPainter* painter, const QStyleOptionGraphicsItem*, QWidget*)
everything began to work as it should (I returned to the old version with double inheritance)
Maybe you had any ideas why? -
Hi,
If you just need the QObject part then your double inheritance is "wrong". QGraphicsObject is a class that inherits from QGraphicsItem and QObject so you can just make yours inherit from QGraphicsPathItem and QObject.
-
All my graphics object should be
QGraphicsObject
becаuse after all, I had that function:QGraphicsObject* QDMGraphicsView::getItemAClick(QMouseEvent *event) { QPoint position = event->pos(); QGraphicsObject* obj = nullptr; if (itemAt(position) != nullptr) { obj = itemAt(position)->toGraphicsObject(); } return obj; }
It is pretty important in programm's logic. Idea is - I clicked at some graphics object and gets theit meta-information in
cin
for example. Whel all my classes inherits fromQGraphicsItem
- it's easy to do. But any ideas what I should do with class that inherits fromQGraphicsPathItem
? -
@Valderman said in Multiple Inheritance from QGraphicsObject and concrete QGraphics*item.:
Whel all my classes inherits from
QGraphicsItem
- it's easy to do. But any ideas what I should do with class that inherits fromQGraphicsPathItem
?I don't understand this question, because
QGraphicsPathItem
already inherits fromQGraphicsItem
....@SGaist said in Multiple Inheritance from QGraphicsObject and concrete QGraphics*item.:
If you just need the QObject part then your double inheritance is "wrong". QGraphicsObject is a class that inherits from QGraphicsItem and QObject so you can just make yours inherit from QGraphicsPathItem and QObject.
He is saying that if all you need from
QGraphicsObject
is theQObject
inheritance (e.g. so you can do signals/slots) you might be better changing your code to do your own multiple inheritance fromSomeGraphicsItem
+QObject
and work offQGraphicsItem
+QObject
base commonality instead ofQGraphicsObject
.