Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QGraphicsItem inheritance issues
Forum Updated to NodeBB v4.3 + New Features

QGraphicsItem inheritance issues

Scheduled Pinned Locked Moved Solved General and Desktop
qgraphicsiteminheritance
6 Posts 3 Posters 838 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    wazzdaman
    wrote on last edited by
    #1

    Hello,

    I'm working on my custom little toy in Qt5.15/C++ which uses my own QGraphicsTextItem, QGraphicsLineItem and QGraphicsRectItem based classes. They share common QGraphicsItem related methods like a method for setting flags, overridden itemChange() 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!

    JonBJ 1 Reply Last reply
    0
    • W wazzdaman

      Hello,

      I'm working on my custom little toy in Qt5.15/C++ which uses my own QGraphicsTextItem, QGraphicsLineItem and QGraphicsRectItem based classes. They share common QGraphicsItem related methods like a method for setting flags, overridden itemChange() 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!

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @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 a QGraphicsItem or possibly make your classes for all of these derive from an extra class with the desired functionality in addition to deriving from QGraphicsItem.

      W 1 Reply Last reply
      0
      • JonBJ JonB

        @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 a QGraphicsItem or possibly make your classes for all of these derive from an extra class with the desired functionality in addition to deriving from QGraphicsItem.

        W Offline
        W Offline
        wazzdaman
        wrote on last edited by
        #3

        @JonB Thanks for your answer! Could you write a short sample code for the second option (not with the helper functions)?

        jsulmJ JonBJ 2 Replies Last reply
        0
        • W wazzdaman

          @JonB Thanks for your answer! Could you write a short sample code for the second option (not with the helper functions)?

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @wazzdaman Why do you need sample code for that? You should know how to subclass a class:

          class MyClass : public QGraphicsItem
          ...
          

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • W wazzdaman

            @JonB Thanks for your answer! Could you write a short sample code for the second option (not with the helper functions)?

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #5

            @wazzdaman
            For the "second option", multiple inheritance, I had in mind you would be using stuff like

            class YourHelperGraphicsItem
            
            class YourGraphicsTextItem : public QGraphicsTextItem, public YourHelperGraphicsItem
            
            class YourGraphicsLineItem : public QGraphicsLineItem, public YourHelperGraphicsItem
            

            for each of the QGraphics...Items you want to use. So that you can put "shared functionality" in YourHelperGraphicsItem. (There are issues, though, e.g. YourHelperGraphicsItem cannot derive from QGraphicsItem.) 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.

            W 1 Reply Last reply
            0
            • JonBJ JonB

              @wazzdaman
              For the "second option", multiple inheritance, I had in mind you would be using stuff like

              class YourHelperGraphicsItem
              
              class YourGraphicsTextItem : public QGraphicsTextItem, public YourHelperGraphicsItem
              
              class YourGraphicsLineItem : public QGraphicsLineItem, public YourHelperGraphicsItem
              

              for each of the QGraphics...Items you want to use. So that you can put "shared functionality" in YourHelperGraphicsItem. (There are issues, though, e.g. YourHelperGraphicsItem cannot derive from QGraphicsItem.) 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.

              W Offline
              W Offline
              wazzdaman
              wrote on last edited by
              #6

              @jsulm Thanks!

              @JonB I see, thanks for the clarification!

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved