Can a method void paint(QPainter * painter,const QStyleOptionGraphicsItem * option,QWidget * widget) be reimplemented
-
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
-
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! -
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 functionvirtual QPainterPath shape() const; // virtual function virtual void paint(QPainter * painter) = 0; // abstract function
@
-
[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.