Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QGraphicsView inside QMainWindow does not work ? (all code snippet pasted)
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Solved General and Desktop
qgraphicsviewqmainwindow
3 Posts 2 Posters 1.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Q Offline
    Q Offline
    qt_fan_4k
    wrote on last edited by
    #1

    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);
    }
    
    
    kshegunovK 1 Reply Last reply
    0
    • Q qt_fan_4k

      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);
      }
      
      
      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by
      #2

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

      Kind regards.

      Read and abide by the Qt Code of Conduct

      1 Reply Last reply
      0
      • Q Offline
        Q Offline
        qt_fan_4k
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved