Results 1 to 7 of 7

Thread: How to render QGraphicsItems onto a QGraphicsScene properly

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,324
    Thanks
    316
    Thanked 871 Times in 858 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to render QGraphicsItems onto a QGraphicsScene properly

    One problem with your code, which in this case probably works but simply by accident, is that you are creating your text item on the stack. It works here because the main() method doesn't exit until the app does, so the text item stays in scope. However, if you do this same thing anywhere else in a method that returns, your text item will be destroyed (and thus removed from the scene) as soon as the method exits. That's a bug waiting to happen.

    On the other hand, you allocate the scene on the heap, the pixmap item on the heap, but the pixmap itself on the stack. It is very inconsistent. The first fix I would make is to allocate everything on the heap using new (except maybe the pixmap itself, since the pixmap item makes a copy) and see if there's a change in behavior.

    You also haven't given any graphics item a position, nor have you set a size for the scene or view. So who knows if this reliance on default behavior is what is causing the problem?

  2. The following user says thank you to d_stranz for this useful post:

    jshafferman (28th April 2014)

  3. #2
    Join Date
    Dec 2010
    Posts
    76
    Thanks
    13
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to render QGraphicsItems onto a QGraphicsScene properly

    Yes I agree I am doing things with the stack and heap that you normally wouldn't do (however as you mentioned it is in the main function so shouldn't be an issue). I was actually 'trying' to break it at first and then I couldn't get it to appear no matter what, so I decided to show you the code as is. I have removed any stack implementation and went strictly to heap calls and I wasn't able to see any difference. I also have set the view's height/width to 600x600 (arbitrary nothing particular) and moved the pixmap to pos(10,10) which should show up since my scene is 600x600 and my image is 200x200. After those few changes no behavior change at all. I also tried creating a simple qgraphicsitem from the example given in the QGraphicsItem documentation and could not get it to appear using a QGLWidget however it appears just fine when using a normal QWidget. I thought maybe for some reason in Qt 4.8 you have to override the paint function or something silly like that but that isn't true. I don't know if this is a bug in Qt 4.8 or not still trying to find out what is wrong with QGraphicsView/Scene with QGLWidget. If anyone has any information that would be great and thanks d_stranz for your comments, any other ideas? I will post any findings on have on this post. Thanks for any help!

  4. #3
    Join Date
    Dec 2010
    Posts
    76
    Thanks
    13
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to render QGraphicsItems onto a QGraphicsScene properly

    It appears to not be an issue with Qt, but rather an issue with the environment that we build in. I was able to get rendering fine with a native Linux machine however with the version of VM player that we currently have there appears to be problems with QGraphicsScene/QGraphicsView rendering properly. We are using a pretty old VM and I am guessing that version's 3D acceleration doesn't actually work properly. I am going to find out if it is possible that the version of ubuntu we run at work also has something to do with it. If this sparks any ideas, please let me know. Thanks!

  5. #4
    Join Date
    Dec 2010
    Posts
    76
    Thanks
    13
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to render QGraphicsItems onto a QGraphicsScene properly

    I know I am posting a lot to myself but I would like people who view this thread to know the problems I am experiencing. So I have the most up to date VM player, the newest Ubuntu 64-bit OS and I installed and used qt 5.2.1 and everything rendered perfect. When I installed 4.8.6 and ran the same code I wasn't able to see the QPixmap render on the screen. However I was able at work to run it on a native Ubuntu system and it rendered fine, so I still believe it has something to do with VM/Ubuntu but it is still interesting to note that 4.8.6 doesn't render the same as 5.2 in the same environment.

Similar Threads

  1. QGraphicsScene and dynamic addition of QGraphicsItems
    By El Bazza in forum Qt Programming
    Replies: 7
    Last Post: 15th September 2012, 07:57
  2. Replies: 1
    Last Post: 16th April 2011, 08:23
  3. Replies: 1
    Last Post: 15th March 2010, 08:51
  4. QGraphicsScene render thread
    By nicolas1 in forum Qt Programming
    Replies: 7
    Last Post: 26th October 2008, 12:57
  5. QGraphicsScene render() crashes
    By iebele in forum Qt Programming
    Replies: 0
    Last Post: 29th April 2008, 13:38

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
  •  
Qt is a trademark of The Qt Company.