Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Solved (Pure) Abstract class that inherits from QGraphicsItem

    General and Desktop
    abstract class inheritance qgraphicsitem custom item
    2
    2
    934
    Loading More Posts
    • 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.
    • Pl45m4
      Pl45m4 last edited by

      Is it possible to make an own custom class abstract, inherit mousePressEvents, paint() and boundingRect() from QGraphicsItem and define concrete classes which inherit from my abstract base class AND still inherit from QGraphicsItem?

      For example:
      MyItem.h

      class MyItem: public QGraphicsItem
      {
      public:
          MyItem();
      
          virtual QRectF boundingRect() const = 0; // pure virtual to override in concrete class
          virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
                     QWidget *widget);
          
      protected:
          void mousePressEvent(QGraphicsSceneMouseEvent *event);
      
      private:
          // my member_vars
      };
      

      Concrete class(es)

      class MyShapeA: public MyItem
      {
      public:
         MyShapeA();
         QRectF boundingRect(.....);
         void paint(......);
      };
      
      class MyShapeB: public MyItem
      {
      public:
          MyShapeB();
      
          QRectF boundingRect(.....);
          void paint(......);
      };
      

      When it's done, I want to generate random (concrete) objects from MyItem / my concrete classes. All concrete classes should have a different boundingRect, paint and members but the same behavior for example on mouseEvents.
      Do I need my abstract base class for that or is it better to let all concrete classes inherit directly from QGrahicsItem and skip the MyItem class between them.

      I have worries that it will cause problems later, because boundingRect is overridden and inherited from QGraphicsItem already (inherit from 2 QObject classes).


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      1 Reply Last reply Reply Quote 0
      • mrjj
        mrjj Lifetime Qt Champion last edited by mrjj

        Hi
        As long as you dont do multiple inheritance ,
        i think you base class design should work just fine.
        However, make sure to overrride
        int QGraphicsItem::type() const pr concrete class to make sure
        qgraphicsitem_cast() works.

        1 Reply Last reply Reply Quote 3
        • First post
          Last post