QGraphicsItem inheritance issues
-
Hello,
I'm working on my custom little toy in Qt5.15/C++ which uses my own
QGraphicsTextItem
,QGraphicsLineItem
andQGraphicsRectItem
based classes. They share commonQGraphicsItem
related methods like a method for setting flags, overriddenitemChange()
implementation and my extra custom methods too.It's not very elegant to have all this code duplicated in my customized classes but all my efforts were unsuccessful due to inheritance issues.
How can I elegantly solve this problem?
Thanks a lot!
-
@wazzdaman
Since C++ does not allow you to alter the base class to extend it, either write "helper" functions/class which take a parameter of aQGraphicsItem
or possibly make your classes for all of these derive from an extra class with the desired functionality in addition to deriving fromQGraphicsItem
. -
@wazzdaman Why do you need sample code for that? You should know how to subclass a class:
class MyClass : public QGraphicsItem ...
-
@wazzdaman
For the "second option", multiple inheritance, I had in mind you would be using stuff likeclass YourHelperGraphicsItem class YourGraphicsTextItem : public QGraphicsTextItem, public YourHelperGraphicsItem class YourGraphicsLineItem : public QGraphicsLineItem, public YourHelperGraphicsItem
for each of the
QGraphics...Item
s you want to use. So that you can put "shared functionality" inYourHelperGraphicsItem
. (There are issues, though, e.g.YourHelperGraphicsItem
cannot derive fromQGraphicsItem
.) That is the only way I know to "extend" the shared functionality in C++ since you cannot alter the base class (QGraphicsItem
). I don't know whether anyone else can achieve something with C++ templates.