PDA

View Full Version : QGraphicsItem paint not triggered



roband915
30th March 2011, 08:01
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?



MyScene::MyScene(QObject *parent)
: QGraphicsScene(parent), currentDrawItem(NULL), bDraw(false)
{
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);
}
}
}





itemSymbols::itemSymbols() : QGraphicsItem()
{
void itemSymbols::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
Debug("itemSymbols paint()");
}
}

wysota
30th March 2011, 09:57
How did you implement boundingRect() for the item?

roband915
30th March 2011, 10:50
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


symbols = new itemSymbols();
QSizeF size(this->sceneRect().width()*0.05,this->sceneRect().height()*0.07);
QPointF centerPos = QPointF(this->sceneRect().center());
symbols->setSize(centerPos, size);
addItem(symbols);


And then in the item itself I do this


void itemSymbols::setSize(QPointF pos, QSizeF size)
{
QRectF test(pos size);
bBox = test;
}
QRectF itemSymbols::boundingRect() const
{
return bBox;
}

wysota
30th March 2011, 11:33
No, this is wrong. If you modify the boundingRect value, you need to inform Graphics View about it by calling QGraphicsItem::prepareGeometryChange().

roband915
30th March 2011, 13:07
Oh ok. Like this?


void itemSymbols::setSize(QPointF pos, QSizeF size)
{
centerPos = pos;
QRectF test(centerPos,size);
QGraphicsItem::prepareGeometryChange();
bBox = test;
}

wysota
30th March 2011, 13:24
Swap #5 and #6, just in case. And of course make sure you paint within the rectangle returned by boundingRect.

roband915
30th March 2011, 13:26
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.

wysota
30th March 2011, 13:27
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.

roband915
31st March 2011, 09:48
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?


void itemSymbols::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
painter->setRenderHint(QPainter::Antialiasing);
qDebug("itemSymbols paint()");

bBox.setHeight(height);
bBox.setWidth(width);

QPointF header;
header.setX(width/2);
header.setY(height*0.05);

qreal newHeight = header.y();

QFont font;
font.setBold(true);
painter->setFont(font);

QPen pen;
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);

QFont font2;
font2.setPointSizeF(4);
painter->setFont(font2);

for(int i = 0;i<vPics.size();i++)
{
QPointF p;
p.setX(10.0);
p.setY(h+10);

QPointF p2;
p2.setX(60.0);
p2.setY(h);

h = h + iRows;
c = c + iCol;

QPixmap pixmap(vPics.at(i));
QRectF d = QRectF(p.x(),p.y(),28,28);
//painter->drawRect(d);
//painter->drawPixmap(p,pixmap);

const char *chTemp;
chTemp = vDesc.at(i).toAscii();
QRectF t = QRectF(p2.x(), p.y(),120,30);
//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);
}

wysota
31st March 2011, 10:06
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.