QgraphicsScene problem



  • I have trouble using the QgraphicsScene in Qt.

    When I am initializing the QGraphicsScene in the main.cpp i am able to use that. But when I am try to add that to an Widget & then add that to the main window its not working, I am not able to view anything. What is the problem?



  • Could you please post your code?



  • You probably have no QGraphicsView for the scene. Try to create a view(it's obvious it should be custom and inherit QGraphicsView class) and then type something like;

    @
    yourView->addScene(yourScene)
    @

    Then add some items into the scene to check if it's working or not.



  • i am using
    @ QGraphicsScene scene;
    scene.setSceneRect(-300, -300, 600, 600);

    scene.setItemIndexMethod(QGraphicsScene::NoIndex);
    

    QGraphicsView view(&scene);
    view.setRenderHint(QPainter::Antialiasing);
    view.setBackgroundBrush(QPixmap(IMG_BACKGROUND));

    view.setCacheMode(QGraphicsView::CacheBackground);
    view.setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate);
    view.setDragMode(QGraphicsView::ScrollHandDrag);

    view.setWindowTitle(QT_TRANSLATE_NOOP(QGraphicsView, "Colliding Mice"));
    view.resize(1000,800);
    view.show();

    QPixmap pm( IMG_BACKGROUND );
    field.setBackgroundBrush( pm );@
    

    inside the widget. And when I am calling the widget in the main window. The widget space is created but so QGraphicsScene is coming.



  • Did you set the parent of the view?



  • how to do that???
    I am only using this much.



  • view->setParent(this)



  • thanks it solved my problem :)



  • [quote author="zgulser" date="1297759295"]view->setParent(this)[/quote]

    Better set the parent in the constructor call:

    @
    QGraphicsView view(&scene, parentPointer);
    // parentPointer could be this (if it's QObject based)
    // or any other QObject that makes sense for your app
    @

    All QObject based classes take a parent pointer as an optional argument.


Log in to reply