PDA

View Full Version : QGraphicsScene::update() has no effect



pmwalk
24th February 2012, 02:32
I can't seem to get QGraphcsScene::update() to reflect any modifications I've made to items in the scene. Here is the shortest example where the problem is replicated:



QGraphicsScene *scene = new QGraphicsScene();
scene->setSceneRect(0, 0, 600, 600);
ui.map_view->setScene(scene);
QRectF *r = new QRectF(0, 0, 100, 100);
scene->addRect(*r, targetPen, targetBrush);
r->setRect(200, 200, 300, 300);
scene->update(scene->itemsBoundingRect());


Because this is all done essentially instantaneously, I should see the 100x100 rectangle at 200, 200. Instead, it remains at 0, 0 (I checked to make sure I wasn't thinking (200, 200) was actually (0, 0)). Any thoughts?

Lykurg
24th February 2012, 06:54
First you don't have to call update if you alter an item, the scene makes that automatically. But you have to alter an item, you just alter a rect, which isn't connected to the item in any way! So do


QGraphicsRectItem *item = scene->addRect(*r, targetPen, targetBrush);
item->setRect(200, 200, 300, 300);

Further for performance reasons create QRect on the stack. And better don't use QRect's x and y when using an item on scene. Define that using the items position (QGraphicsRectItem::setPos()).

pmwalk
24th February 2012, 07:48
Thanks for the help! That seems to have done the trick.

Now I'm having a problem when altering an image. I have the following code:



QImage *mapImage = new QImage(600, 600, QImage::Format_RGB16);
mapImage->fill(unknown);
QGraphicsPixmapItem mapPixmap(QPixmap::fromImage(*mapImage));
scene->addItem(&mapPixmap);


I decided to check and make sure the image was initialized properly, and noticed that the QImage::isNull() has some strange behavior. When I run the following loop:



std::cout << "Begin test" << std::endl;
if (mapImage->isNull()) {
std::cout << "NULL";
}
else {
std::cout << "NOT NULL";
}


The "Begin test" will print out, but then nothing else. However, when I add a "std::cout << "End test" at the end, "NOT NULL" will print out (which also means my QImage is initialized and should have valid pixel values). I'm a bit new to Qt (and somewhat new to C++), but I can't find a good explanation of this problem anywhere else.

Blackened Justice
24th February 2012, 09:02
It's probably not printing out anything because you're not flushing the output buffer. Try adding "<< std::endl" to the "NULL" and "NOT NULL" std::coutcout lines.

pmwalk
24th February 2012, 16:10
Ahh of course. That fixed it.

I still have the problem with the graphics scene not displaying the image though.