Results 1 to 6 of 6

Thread: Adding QgraphicsScene to my QgraphicsView

  1. #1
    Join Date
    May 2011
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Adding QgraphicsScene to my QgraphicsView

    Hello everyone,

    I have a problem. I'm trying to add a QgraphicsScene to my QgraphicsView (that I adding in QT designer) If I construct a QgraphicsView from code it works fine (It will create a new window though and I don't need that.)

    My QgraphicsView that I make in Designer is called game_view i'm trying to add the view as such... game_view->setScene(&scene)

    When I do this I don't have any errors but nothing is appearing in the view!

    Any help is good help

    Thank you
    Chris

  2. #2
    Join Date
    Mar 2011
    Posts
    63
    Thanks
    11
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Adding QgraphicsScene to my QgraphicsView

    Well do you have any items in your scene that should show up? Otherwise the scene it self is just blank white. Also how do you create the scene?

  3. #3
    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: Adding QgraphicsScene to my QgraphicsView

    "&scene" leads only to one conclusion (most likely): You created the scene on the stack, thus it is getting destroyed at the end of the function. Allocate the scene on the heap or as a member variable of the class which contains the view.

  4. #4
    Join Date
    May 2011
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Adding QgraphicsScene to my QgraphicsView

    Yes the scene is populated...

    I'm a bit confused by what you mean this is part of my code...

    I borrowed some code from the QT examples for this,

    Qt Code:
    1. #include <QtGui>
    2. #include <QPrinter>
    3. #include <QGraphicsScene>
    4. #include <QStyleOption>
    5. #include <math.h>
    6. #include "myqtapp.h"
    7.  
    8. #include "mouse.h"
    9.  
    10. static const int MouseCount = 2;
    11.  
    12. myQtApp::myQtApp(QWidget*)
    13. {
    14. setupUi(this);
    15. connect( pushButton_newgame, SIGNAL( clicked() ), this, SLOT( newgame() ) );
    16. scene.setSceneRect(-100, -100, 200, 200);
    17. scene.setItemIndexMethod(QGraphicsScene::NoIndex);
    18.  
    19. for (int i = 0; i < MouseCount; ++i) {
    20. Mouse *mouse = new Mouse;
    21. mouse->setPos(::sin((i * 6.28) / MouseCount) * 200,
    22. ::cos((i * 6.28) / MouseCount) * 200);
    23. scene.addItem(mouse);
    24. }
    25.  
    26.  
    27. //QGraphicsView view(&scene);
    28. game_view->setScene(&scene);
    29. //game_view->setRenderHint(QPainter::Antialiasing);
    30. //game_view->setBackgroundBrush(QPixmap(":/images/cheese.jpg"));
    31. //game_view->setCacheMode(QGraphicsView::CacheBackground);
    32. //game_view->setDragMode(QGraphicsView::ScrollHandDrag);
    33. //game_view->setWindowTitle(QT_TRANSLATE_NOOP(QGraphicsView, "Colliding Mice"));
    34. //game_view->resize(400, 300);
    35. //game_view->show();
    36. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 13th May 2011 at 12:19. Reason: missing [code] tags

  5. #5
    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: Adding QgraphicsScene to my QgraphicsView

    Yeah, it is exactly like I said. Your scene is deleted at the end of the constructor. So you can not expect to see something that already gets deleted. It's all about basic C++ knowledge: Live time of objects. Feel free to contact any c++ reference. And please use the code tags next time.

  6. #6
    Join Date
    May 2011
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Adding QgraphicsScene to my QgraphicsView

    Thank you, found my issues and fixed them
    Chris

Similar Threads

  1. Adding QGLWidget to QGraphicsScene problem
    By sanjayshelke in forum Qt Programming
    Replies: 10
    Last Post: 26th April 2016, 15:03
  2. adding subclassed items to a QGraphicsScene
    By liqxpil in forum Newbie
    Replies: 3
    Last Post: 27th October 2010, 11:54
  3. Adding QGraphicsProxyWidget items into QGraphicsScene
    By yagabey in forum Qt Programming
    Replies: 3
    Last Post: 21st November 2008, 06:33
  4. Adding items to a QGraphicsScene's selection list?
    By mooreaa in forum Qt Programming
    Replies: 2
    Last Post: 1st July 2008, 20:01
  5. Replies: 0
    Last Post: 7th April 2008, 14:27

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.