Results 1 to 7 of 7

Thread: How to render QGraphicsItems onto a QGraphicsScene properly

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

    Default How to render QGraphicsItems onto a QGraphicsScene properly

    I have a custom QGraphicsScene where I reimplement the drawbackground function and draw using opengl. I am rendering everything as expected in drawbackground. However when I add a QGraphicsItem to the scene I seem to be having difficulty getting the items to render appropriately. I am able to render a QGraphicsSimpleTextItem fine, however not so much with a QGraphicsPixmapItem. If I try to just add the QGraphicsPixmapItem nothing appears on the screen, however if I add both the QGraphicsSimpleTextItem and QGraphicsPixmapItem both render fine. I have no idea why this is happening, so I am hoping by taking a look at my main function anyone might be able to steer me straight. This is just a small program designed to teach myself how to properly add items to a QGraphicsScene.

    Qt Code:
    1. #include "glgraphicsscene.h"
    2.  
    3. #include <QApplication>
    4. #include <QGraphicsView>
    5. #include <QGraphicsSimpleTextItem>
    6. #include <QGraphicsPixmapItem>
    7. #include <QPixmap>
    8. #include <QDebug>
    9.  
    10. int main(int argc, char *argv[])
    11. {
    12. QApplication app(argc, argv);
    13.  
    14. GLGraphicsScene* glScene = new GLGraphicsScene;
    15.  
    16. QGraphicsSimpleTextItem textItem("Hello QGraphicsItem!");
    17. textItem.setBrush(Qt::black);
    18.  
    19. QPixmap pixmap;
    20.  
    21. qDebug() << pixmap.load("../Bounce/graphicsview-pixmapitem.png");
    22. qDebug() << pixmap.isNull();
    23. qDebug() << pixmap.width();
    24. qDebug() << pixmap.height();
    25. qDebug() << pixmap.hasAlpha();
    26.  
    27. QGraphicsPixmapItem* gpixmap = new QGraphicsPixmapItem(pixmap);
    28. gpixmap->setVisible(true);
    29.  
    30. // this works fine all the time
    31. glScene->addItem(&textItem);
    32.  
    33. // this won't render unless line above is done first
    34. glScene->addItem(gpixmap);
    35.  
    36. // just to find out if items are being added for my benefit
    37. for(int i = 0; i < glScene->items().size(); i++)
    38. {
    39. qDebug() << glScene->items().at(i);
    40. }
    41.  
    42. view.setViewport(new QGLWidget(QGLFormat(QGL::DoubleBuffer | QGL::Rgba)));
    43. view.setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
    44. view.setScene(glScene);
    45. view.show();
    46.  
    47. app.exec();
    48. }
    To copy to clipboard, switch view to plain text mode 

    Any ideas, I haven't found anything on the internet to tell me why this won't work. Any help is greatly appreciated!

  2. #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

    Just an FYI, if I strictly just add the QPixmap to the scene and the view is a regular QWidget. It renders fine... I have looked through every bit of information I can and I have yet to see anything that relates to my current problem. If anyone needs more implementation shown that is fine with me, and I can post the drawBackground call if necessary. Thanks for any help because I am completely lost

  3. #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

    One more additional FYI, I put plain QGraphicsScene added a QPixmap/QGraphicsPixmapItem to it, created a QGraphicsView and set its viewport to a QGLWidget and the Pixmap would not render on the screen. This is leading me to believe that there is sometype of bug in Qt. Maybe it can't be directly applied, maybe it has to be a subclassed QGraphicsItem and in the paint function I just make the pixmap image? I am not sure just trying to find out what exactly has to happen in order for this work without sometype of silly hack (adding a QGraphicsSimpleText item first or something silly like that). Thanks again for any help. Oh and I am using Qt 4.8.7, I am going to see if it is fixed with Qt 5.2


    Added after 8 minutes:


    Quick side note, this appears to be fixed in 5.2, however at my office we use 4.8.7, so I am still curious if this is something that is documented somewhere.
    Last edited by jshafferman; 26th April 2014 at 14:58.

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 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?

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

    jshafferman (28th April 2014)

  6. #5
    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!

  7. #6
    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!

  8. #7
    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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.