QGraphicsItem inheritance issues
-
Hello,
I'm working on my custom little toy in Qt5.15/C++ which uses my own
QGraphicsTextItem,QGraphicsLineItemandQGraphicsRectItembased classes. They share commonQGraphicsItemrelated 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!
-
Hello,
I'm working on my custom little toy in Qt5.15/C++ which uses my own
QGraphicsTextItem,QGraphicsLineItemandQGraphicsRectItembased classes. They share commonQGraphicsItemrelated 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 aQGraphicsItemor possibly make your classes for all of these derive from an extra class with the desired functionality in addition to deriving fromQGraphicsItem. -
@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 aQGraphicsItemor possibly make your classes for all of these derive from an extra class with the desired functionality in addition to deriving fromQGraphicsItem. -
@JonB Thanks for your answer! Could you write a short sample code for the second option (not with the helper functions)?
@wazzdaman Why do you need sample code for that? You should know how to subclass a class:
class MyClass : public QGraphicsItem ... -
@JonB Thanks for your answer! Could you write a short sample code for the second option (not with the helper functions)?
@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 YourHelperGraphicsItemfor each of the
QGraphics...Items you want to use. So that you can put "shared functionality" inYourHelperGraphicsItem. (There are issues, though, e.g.YourHelperGraphicsItemcannot 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. -
@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 YourHelperGraphicsItemfor each of the
QGraphics...Items you want to use. So that you can put "shared functionality" inYourHelperGraphicsItem. (There are issues, though, e.g.YourHelperGraphicsItemcannot 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.