PDA

View Full Version : inherite QGraphicsItem inherited class



aremar
24th November 2010, 17:39
Hi,
I'm trying make a small paint program in Qt. I have the a problem with making a effect to show when my item is selected. Because I dont want to implement an effect for all of my items (cercle, rectangle ...), also how they must resize...
I decided to inherite QGraphics item for my class MyGraphicsItem. In this class I want to create a member QGraphicsEffect and a one or two methods for that how to show , where to show.... Than i want to inherite MyGraphicsItem and to use it with different shapes. The problem is that I'm not sure what to do with paint and boundingRect in MyGraphicsItem. But I want to use them in the methods.

I'm sorry for my poor english.

P.S. Also open for other solutions.

franz
24th November 2010, 21:34
I wouldn't re-implement those functions until I reach a subclass where I know what to do. Just call them in your code. The inheritance mechanism will take care of executing the proper implementation.

aremar
25th November 2010, 17:16
I found a solution. This is tha way i did it:


#ifndef BASEGRAPHICSITEM_H
#define BASEGRAPHICSITEM_H

#include <QGraphicsItem>
class QGraphicsDropShadowEffect;

class BaseGraphicsItem : public QGraphicsItem
{

public:
explicit BaseGraphicsItem(QGraphicsItem *parent = 0);

void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);



signals:

public slots:

private:
QGraphicsDropShadowEffect *effect;

};

#endif // BASEGRAPHICSITEM_H





#include "basegraphicsitem.h"
#include <QGraphicsDropShadowEffect>
#include <QGraphicsItem>
#include <QPainter>

BaseGraphicsItem::BaseGraphicsItem(QGraphicsItem *parent) :
QGraphicsItem(parent)
{
effect = new QGraphicsDropShadowEffect();
effect->setBlurRadius(10);
effect->setColor(Qt::black);
this->setGraphicsEffect(effect);




}

void BaseGraphicsItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{

QRectF rect = boundingRect();
QPointF point = rect.center();

painter->setPen(Qt::NoPen);
painter->setBrush(QBrush(Qt::red));
painter->drawEllipse(point,3,3);
painter->setBrush(Qt::NoBrush);
painter->setPen(Qt::black);



}



and I even can use the base class paint from the derived like this
BaseGraphicstem::paint();

marcvanriet
26th November 2010, 13:26
Hi,

Did you look into the 'Diagram Scene' example that comes with Qt ?
I think this one already does most of the things you need.

Best regards,
Marc

aremar
27th November 2010, 19:34
Thank you for the example. I've looked at many different examples, but this will help me a lot.