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. Can a method void paint(QPainter * painter,const QStyleOptionGraphicsItem * option,QWidget * widget) be reimplemented
Qt 6.11 is out! See what's new in the release blog

Can a method void paint(QPainter * painter,const QStyleOptionGraphicsItem * option,QWidget * widget) be reimplemented

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 4.6k Views 1 Watching
  • 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.
  • N Offline
    N Offline
    NarutoUzumakis
    wrote on last edited by
    #1

    Hi there,i have a short question.If i have one class which inherits from QGraphicsItem and i want to make it the base class for some other items which will inherit from it,can i make the paint method and other methods for drawing graphics items virtual>I want to make the Item class abstract and then i want other items to inherit from it.Can it be done,I tried defining the methods abstract but it didn't work..,,
    Code example
    @
    class Item : public QGraphicsItem
    {
    public:

    QRectF boundingRect() const;
    
    QPainterPath shape() const;
    
    void paint(QPainter * painter,
               const QStyleOptionGraphicsItem * option,
               QWidget * widget);
    

    @

    So,is it possible to make these 3 methods virtual and to redefine them in subclasses of this class

    Thaks

    1 Reply Last reply
    0
    • L Offline
      L Offline
      loladiro
      wrote on last edited by
      #2

      They are virtual anyway. Let wikipedia give you an "example":http://en.wikipedia.org/wiki/Virtual_function
      If you did something like this in the main() method of the example
      @
      static_cast<Fish*>(new GoldFish())->eat();
      @
      the output would still be "I eat like a goldfish!".
      So you don't have to do anything!

      1 Reply Last reply
      0
      • G Offline
        G Offline
        giesbert
        wrote on last edited by
        #3

        Methods that are virtual in a base class are virtual in all dervied classes, that's by definition, regardeless whether you write virtual in front of them in the derived classes or not.

        What I don't understand is, why you need an intermediate class which adds no functionality and no specific interfaces.

        Just as a side node, an abstract method is defined like below:

        bq. from wikipedia:
        An abstract method is a dummy code method which has no implementation. It is often used as a placeholder to be overridden later by a subclass of or an object prototyped from the one that implements the abstract method. In this way, abstract methods help to partially specify a framework.

        @
        class MyClass
        {
        public:
        QRectF boundingRect() const; // normal function

        virtual QPainterPath shape() const;    // virtual function
        
        virtual void paint(QPainter * painter) = 0;     // abstract function
        

        @

        Nokia Certified Qt Specialist.
        Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

        1 Reply Last reply
        0
        • L Offline
          L Offline
          loladiro
          wrote on last edited by
          #4

          [quote author="Gerolf" date="1308505407"]
          An abstract method is a dummy code method which has no implementation. It is often used as a placeholder to be overridden later by a subclass of or an object prototyped from the one that implements the abstract method. In this way, abstract methods help to partially specify a framework.
          [/quote]

          Just wanted to add to that, that it is possible to actually implement pure virtual functions and then call them from the derived class (in which you still have to implement it though). So a function can be pure virtual, but not abstract.

          Another thing as to not confuse the OP,
          @
          QRectF QGraphicsItem::boundingRect () const
          @
          is pure virtual in QGraphicsItem and was just used as an example by Gerolf.

          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