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
QtWS25 Last Chance

QGraphicsItem inheritance issues

Scheduled Pinned Locked Moved Solved General and Desktop
qgraphicsiteminheritance
6 Posts 3 Posters 791 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 30 Dec 2022, 10:36 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!

    J 1 Reply Last reply 30 Dec 2022, 12:15
    0
    • W wazzdaman
      30 Dec 2022, 10:36

      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!

      J Online
      J Online
      JonB
      wrote on 30 Dec 2022, 12:15 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 13 Jan 2023, 05:34
      0
      • J JonB
        30 Dec 2022, 12:15

        @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 13 Jan 2023, 05:34 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)?

        J J 2 Replies Last reply 13 Jan 2023, 06:10
        0
        • W wazzdaman
          13 Jan 2023, 05:34

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

          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 13 Jan 2023, 06:10 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
            13 Jan 2023, 05:34

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

            J Online
            J Online
            JonB
            wrote on 13 Jan 2023, 13:12 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 14 Jan 2023, 17:50
            0
            • J JonB
              13 Jan 2023, 13:12

              @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 14 Jan 2023, 17:50 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