Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QGraphicsView inside QMainWindow does not work ? (all code snippet pasted)



  • I've no idea why the main window does not display the intended text, yet only blank ?

    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
    {
        QGraphicsScene scene;
        scene.addText("Hello, world!");
    
        QGraphicsView *view = new QGraphicsView(this);
        view->setScene(&scene);
        this->setCentralWidget(view);
    }
    
    

  • Moderators

    @qt_fan_4k
    Hello,
    Your scene object is on the stack, so it will be destroyed when the constructor exits.

    Kind regards.



  • Right.
    This is not the first time I made such mistake. So the lifecycle of GraphicsScene is required for graphics view.

    Thanks for the help. This is solved.