PDA

View Full Version : Trouble painting customized QGraphicsPathItem



jkutchka
14th June 2016, 19:15
Hello,
I am trying to create a class that inherits from QGraphicsPathItem so that the class can have some custom data members. This will be helpful when selecting an item on the scene with a mouse click and then displaying or changing attributes of that selected item.
The code that adds a new path to to a view is:


void processPath(QPainterPath newPath)
{
newPath = mapToScene(newPath);
QGraphicsPathItem *newItem = new QGraphicsPathItem(newPath);
newItem->setPen(ItemPen);
newItem->setBrush(ItemFill);
CareZone *newCZ = new CareZone(newItem);
CZGraphicsItem *newCZGI = new CZGraphicsItem(newItem,newCZ);

scene()->addItem(newCZGI);
carezones.push_back(newCZ);
viewport()->update();
}


If I change scene()->addItem(newCZGI) to scene()->addItem(newItem) it works but then it is using a QGraphicsPathItem and not a the customized CZGraphicsItem. CareZone is a class that contains data members. CZGraphicsItem contains a pointer to it.

I have added paint() and boundingRect() functions to the CZGraphicsItem class.

The paint method is :


void paint(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget = 0)
{
qDebug() << "paint called" ;
painter->setPen(gpi->pen());
painter->setBrush(gpi->brush());
painter->drawPath(gpi->shape());
QGraphicsPathItem::paint(painter,option,widget);
}

I think it should use the same paint method used by QGraphicsPathItem but I do not know how to do this. The same is true for boundingRect().
The boundingRect method is:


QRectF boundingRect()
{
qDebug() << "boundRect() called";
return gpi->boundingRect();
}

gpi is the QGraphicsPathItem passed in the constructor (newItem in the first snippet of code).

I am not seeing either debugging message when running the applications. I do see a debug message that I placed in main though.

Thanks,
jkutchka

anda_skoa
15th June 2016, 08:31
If CZGraphicsItem is a QGraphicsPathItem, why doesn't it get a path?
What do you create newItem for?
Why are you passing it to CZGraphicsItem's constructor?

Cheers,
_

jkutchka
15th June 2016, 16:49
The problem was in the CZGraphicsItem constructor. I had:
CZGraphicsItem(QGraphicsPathItem *GPI, CareZone *cz) :
QGraphicsPathItem(GPI) expecting a copy of the QGraphicsPathItem. Changing it to:
CZGraphicsItem(QGraphicsPathItem *GPI, CareZone *cz) :
QGraphicsPathItem(GPI->shape()) fixed the problem.

anda_skoa
16th June 2016, 10:33
Why not just pass the path?

Cheers,
_