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:
{
newPath = mapToScene(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();
}
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();
}
To copy to clipboard, switch view to plain text mode
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 :
{
qDebug() << "paint called" ;
painter->setPen(gpi->pen());
painter->setBrush(gpi->brush());
painter->drawPath(gpi->shape());
}
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);
}
To copy to clipboard, switch view to plain text mode
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:
{
qDebug() << "boundRect() called";
return gpi->boundingRect();
}
QRectF boundingRect()
{
qDebug() << "boundRect() called";
return gpi->boundingRect();
}
To copy to clipboard, switch view to plain text mode
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
Bookmarks