QGraphicsItem paint not triggered
If I add my subclassed QGraphicsItem before I add anything else to the scene everything works fine. But if I add something else before then the paint method is never triggered in my new QGraphicsItem. Can somebody explain what I am missing?
Code:
{
void MyScene::toggleSymbols()
{
if(bSymbols)
{
qDebug("false");
bSymbols = false;
}
else
{
qDebug("true");
bSymbols = true;
symbols = new itemSymbols(); //Subclassed QGraphicsItem
QPointF centerPos
= QPointF(this
->sceneRect
().
width()/2, this
->sceneRect
().
height()/2);
symbols->setCenterPos(centerPos);
addItem(symbols);
}
}
}
Code:
{
{
Debug("itemSymbols paint()");
}
}
Re: QGraphicsItem paint not triggered
How did you implement boundingRect() for the item?
Re: QGraphicsItem paint not triggered
I've modified the code a bit know but still the same result.
In MyScene.cpp I create the Item and set it´s size like this
Code:
symbols = new itemSymbols();
QSizeF size
(this
->sceneRect
().
width()*0.05,this
->sceneRect
().
height()*0.07);
symbols->setSize(centerPos, size);
addItem(symbols);
And then in the item itself I do this
Code:
{
bBox = test;
}
QRectF itemSymbols
::boundingRect() const {
return bBox;
}
Re: QGraphicsItem paint not triggered
No, this is wrong. If you modify the boundingRect value, you need to inform Graphics View about it by calling QGraphicsItem::prepareGeometryChange().
Re: QGraphicsItem paint not triggered
Oh ok. Like this?
Code:
{
centerPos = pos;
bBox = test;
}
Re: QGraphicsItem paint not triggered
Swap #5 and #6, just in case. And of course make sure you paint within the rectangle returned by boundingRect.
Re: QGraphicsItem paint not triggered
Ok thanks alot wysota. It still isn't working but now the paint method is triggered so hopefully it's not that hard no solve.
Re: QGraphicsItem paint not triggered
What isn't working? From what I see you're not painting anything. Furthermore it seems your item should probably be derived from QGraphicsRectItem instead of QGraphicsItem.
Re: QGraphicsItem paint not triggered
This is my paint method. What I'm really trying to do here is a movable legend. As you can see the pen size is way off but any less than 200 is not visible so my guess is that I've mixed up the maybe the scene's coordinatesystem with the item's?
Code:
{
painter
->setRenderHint
(QPainter::Antialiasing);
qDebug("itemSymbols paint()");
bBox.setHeight(height);
bBox.setWidth(width);
header.setX(width/2);
header.setY(height*0.05);
qreal newHeight = header.y();
font.setBold(true);
painter->setFont(font);
pen.setWidth(200);
painter->setPen(pen);
painter->drawText(header,"Symbols");
qreal iRows = (height-newHeight)/15;
qreal h = iRows;
qreal iCol = width/4;
qreal c = iCol;
font.setBold(false);
painter->setFont(font);
font2.setPointSizeF(4);
painter->setFont(font2);
for(int i = 0;i<vPics.size();i++)
{
p.setX(10.0);
p.setY(h+10);
p2.setX(60.0);
p2.setY(h);
h = h + iRows;
c = c + iCol;
//painter->drawRect(d);
//painter->drawPixmap(p,pixmap);
const char *chTemp;
chTemp = vDesc.at(i).toAscii();
//painter->drawRect(t);
//painter->drawText(QRectF(p2.x(), p.y(),120,30), Qt::AlignCenter, QObject::tr(chTemp));
}
qDebug("Drawing a box at pos %f %f", bBox.topLeft().x(), bBox.topLeft().y());
painter->drawRect(bBox);
painter->drawPoint(centerPos);
}
Re: QGraphicsItem paint not triggered
I don't know what width and height are but changing the bounding box in the paint routine is definitely a bad idea. The method is called paint() and not paintAndUpdateBoundingBox(). Not knowing the scene size and the transformation matrix of the view I'm unable to say anything about the pen size.