PDA

View Full Version : Updating the width/height of a QGraphicsItem



blooglet
10th February 2011, 13:34
I'm trying to update the width and height of a QGraphicsItem. I've added QGraphicsItem* testItem to a graphics scene using the addItem function.

The documentation says I can use the update function to change the width and height, but the item doesn't change.


int newWidth = /* something */;
int newHeight = /* something */;
QRectF newRect = QRectF(testItem->x(), testItem->y(), newWidth, newHeight);
testItem->update(newRect);

QMessageBox msg1;
msg1.setText("Update successful? " + QString((testItem->boundingRect().width() == width) ? "true" : "false") + QString("\n\nRequested size: ") + QString::number(newWidth) + " x " + QString::number(newHeight) + "\nActual size: " + QString::number(testItem->boundingRect().width()) + " x " + QString::number(testItem->boundingRect().height()));
msg1.exec();

http://i.imgur.com/Oo9BA.png

Why isn't it updating correctly?

JohannesMunk
10th February 2011, 14:03
You misunderstood the rect parameter to the update function. With it you can specify a sub-rectangle that needs updating. In contrast to updating the whole item with update().

Look into QGraphicsWidget to have an item with a size. Otherwise you need to store your own geometry data and report it back through the boundingRect() function.

HIH

Joh