Results 1 to 14 of 14

Thread: Drawing the background of QGraphicsView

  1. #1
    Join Date
    Feb 2007
    Posts
    5
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Drawing the background of QGraphicsView

    I'm trying to change the background in my QGraphicsView after it is initially created, but in QGraphicsView.drawBackground only part of the view is drawn. How do I trigger a repaint of the whole view (or certain parts of it)?

  2. #2
    Join Date
    Jan 2006
    Posts
    128
    Thanked 28 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Drawing the background of QGraphicsView

    QWidget::update() will help you there. (Since QGraphicsView is a child of QWidget)

    The coordinates are in "View Coordinates" which you might have to map from "Scene Coordinates" via QGraphicsView::mapFromScene()

  3. #3
    Join Date
    Feb 2007
    Posts
    5
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Drawing the background of QGraphicsView

    I tried update() on the whole view I think, but still only part of the view was scheduled for background drawing. I'm not at work, so I don't have the actual program in front of me, but I'm pretty sure I tried what you're proposing. Are you sure this should work?

  4. #4
    Join Date
    Jan 2006
    Posts
    128
    Thanked 28 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Drawing the background of QGraphicsView

    Well, it should, unless graphicsview is optimizing a bit (more than you would like)...and it seems to be (i.e. caching). But one look at the documentation revealed the nice solution to your problem: QGraphicsView::resetCachedContent()

  5. #5
    Join Date
    Feb 2007
    Posts
    5
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Drawing the background of QGraphicsView

    I did try resetCachedContent(), but it did nothing really. Besides, caching is turned off by default according to Assistant.

  6. #6
    Join Date
    Oct 2006
    Posts
    60
    Thanks
    9
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: Drawing the background of QGraphicsView

    My GV is created in a UI file and then as part of my program's init i set a brackground. This is the code I use:

    Qt Code:
    1. QString fieldImage = ":/images/Field.png";
    2. scene = new QGraphicsScene();
    3. scene->setSceneRect(0, 0, fieldWidth, fieldHeight);
    4. fieldGV->setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
    5. fieldGV->setBackgroundBrush(QImage(fieldImage));
    6. fieldGV->setCacheMode(QGraphicsView::CacheNone);
    7. fieldGV->setScene(scene);
    To copy to clipboard, switch view to plain text mode 

    Hope it helps.

    mAx

  7. #7
    Join Date
    Jan 2006
    Posts
    128
    Thanked 28 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Drawing the background of QGraphicsView

    Quote Originally Posted by aknuds1 View Post
    I did try resetCachedContent(), but it did nothing really.
    Could you post a little programm (i.e. minimal but runable ;-) That shows the behaviour? That makes helping easier :-)

  8. #8
    Join Date
    Feb 2007
    Posts
    5
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Drawing the background of QGraphicsView

    Quote Originally Posted by camel View Post
    Could you post a little programm (i.e. minimal but runable ;-) That shows the behaviour? That makes helping easier :-)
    I'll see what I can do tomorrow.

  9. #9
    Join Date
    Feb 2007
    Posts
    5
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Drawing the background of QGraphicsView

    Quote Originally Posted by maxpower View Post
    My GV is created in a UI file and then as part of my program's init i set a brackground. This is the code I use:

    Qt Code:
    1. QString fieldImage = ":/images/Field.png";
    2. scene = new QGraphicsScene();
    3. scene->setSceneRect(0, 0, fieldWidth, fieldHeight);
    4. fieldGV->setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
    5. fieldGV->setBackgroundBrush(QImage(fieldImage));
    6. fieldGV->setCacheMode(QGraphicsView::CacheNone);
    7. fieldGV->setScene(scene);
    To copy to clipboard, switch view to plain text mode 

    Hope it helps.

    mAx
    Thanks for the tip, but the problem is rather that I want to trigger redrawing of the background at some point. Initial drawing of the background is no problem.

  10. #10
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Drawing the background of QGraphicsView

    do the image width and height and Scene rect width/height match ??

    might be thers some scaling problem ???

  11. #11
    Join Date
    Oct 2006
    Posts
    60
    Thanks
    9
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: Drawing the background of QGraphicsView

    Okay, but why can't you use setBackGroundBrush() again? I can call that from anywhere in my app and the background changes just fine. I guess it depends on exactly what you are trying to draw as the background.

    mAx

  12. #12
    Join Date
    May 2006
    Location
    Bangalore,India
    Posts
    235
    Thanks
    7
    Thanked 25 Times in 24 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: Drawing the background of QGraphicsView

    workaround.
    just add one item to scene and remove, this will redraw the background.

  13. #13
    Join Date
    Jan 2006
    Location
    Norway
    Posts
    124
    Thanked 38 Times in 30 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Drawing the background of QGraphicsView

    Or define the scene rect, no workaround needed. ;-)
    Bitto / Andreas Aardal Hanssen - andreas dot aardal dot hanssen at nokia
    Nokia Software Manager, Qt Development

  14. #14
    Join Date
    Feb 2007
    Posts
    16
    Thanks
    1
    Platforms
    Windows

    Default Re: Drawing the background of QGraphicsView

    is it a partial background refresh where it just refreshes the scene and leaves everything outside the scene the old background color or does it just plain not work?

    You might also try updating to 4.2.3 since it looks like they did something on this...

    "General improvements
    --------------------

    - Graphics View

    * Calling QGraphicsScene::update() without arguments now correctly
    updates the entire scene.
    * Changing the background brush in QGraphicsScene now correctly updates
    the entire scene
    "
    Last edited by giverson; 9th March 2007 at 15:07.

Similar Threads

  1. Replies: 3
    Last Post: 8th December 2006, 18:51
  2. QFrame and its background
    By high_flyer in forum Qt Programming
    Replies: 2
    Last Post: 23rd June 2006, 20:32
  3. QLabel background color
    By munna in forum Newbie
    Replies: 3
    Last Post: 1st May 2006, 15:36
  4. background colour
    By kw in forum Qt Programming
    Replies: 6
    Last Post: 11th April 2006, 00:44
  5. Replies: 1
    Last Post: 5th April 2006, 16:44

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.