Results 1 to 5 of 5

Thread: QGraphicsScene::update() has no effect

  1. #1
    Join Date
    Feb 2012
    Location
    Pittsburgh, Pennsylvania, USA
    Posts
    10
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QGraphicsScene::update() has no effect

    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:

    Qt Code:
    1. scene->setSceneRect(0, 0, 600, 600);
    2. ui.map_view->setScene(scene);
    3. QRectF *r = new QRectF(0, 0, 100, 100);
    4. scene->addRect(*r, targetPen, targetBrush);
    5. r->setRect(200, 200, 300, 300);
    6. scene->update(scene->itemsBoundingRect());
    To copy to clipboard, switch view to plain text mode 

    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?

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QGraphicsScene::update() has no effect

    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

    Qt Code:
    1. QGraphicsRectItem *item = scene->addRect(*r, targetPen, targetBrush);
    2. item->setRect(200, 200, 300, 300);
    To copy to clipboard, switch view to plain text mode 

    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()).

  3. #3
    Join Date
    Feb 2012
    Location
    Pittsburgh, Pennsylvania, USA
    Posts
    10
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QGraphicsScene::update() has no effect

    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:

    Qt Code:
    1. QImage *mapImage = new QImage(600, 600, QImage::Format_RGB16);
    2. mapImage->fill(unknown);
    3. QGraphicsPixmapItem mapPixmap(QPixmap::fromImage(*mapImage));
    4. scene->addItem(&mapPixmap);
    To copy to clipboard, switch view to plain text mode 

    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:

    Qt Code:
    1. std::cout << "Begin test" << std::endl;
    2. if (mapImage->isNull()) {
    3. std::cout << "NULL";
    4. }
    5. else {
    6. std::cout << "NOT NULL";
    7. }
    To copy to clipboard, switch view to plain text mode 

    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.

  4. #4
    Join Date
    Feb 2012
    Posts
    11
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QGraphicsScene::update() has no effect

    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.

  5. #5
    Join Date
    Feb 2012
    Location
    Pittsburgh, Pennsylvania, USA
    Posts
    10
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QGraphicsScene::update() has no effect

    Ahh of course. That fixed it.

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

Similar Threads

  1. Update QGraphicsScene - Best Practice
    By vinaykusta in forum Qt Programming
    Replies: 0
    Last Post: 4th November 2011, 04:52
  2. QGraphicsScene's sceneRect() doesn't update
    By blooglet in forum Qt Programming
    Replies: 2
    Last Post: 17th February 2011, 09:11
  3. Performance of scene(QGraphicsScene) with update();
    By mukunda in forum Qt Programming
    Replies: 4
    Last Post: 14th January 2011, 12:21
  4. Can't manually update QGraphicsScene
    By aladagemre in forum Qt Programming
    Replies: 4
    Last Post: 17th January 2010, 21:52
  5. QGraphicsScene update?
    By Gurdt in forum Qt Programming
    Replies: 11
    Last Post: 17th April 2009, 09:03

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.