-2

I'm going to draw a QLine in a QGraphicsScene and display it via a QGraphicsView.

The line is drawn during mainwindow construction, but deleted after leaving the MainWindow constructor shortly after been drawn and before reaching any slot. (I noticed this behaviour during debugging.)

The most relevant code lines are:

MainWindow::MainWindow(QWidget* parent):
    ui{new Ui::MainWindow},
    scene{new QGraphicsScene(this)},
    view{new ClickableMap(scene)}, / ... */
{
    ui->setupUi(this);
    ui->view->setScene(scene);
    for (/* ... */ ) {
        QGraphicsItem* edgeDrawing= scene->addLine(x1, y1, x2, y2);
        edgeDrawing->setZValue(1);
    }
    ui->view->show();
}

Why exactly is the drawing hidden? It's no problem to draw by using the signal-slot concept (mouse click on the QGraphicsView), but I would like to display the drawing at program start.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
user7427029
  • 255
  • 2
  • 12
  • show ClickableMap class – eyllanesc Dec 10 '18 at 21:42
  • Is the view relevant?, remember that view and ui->view are 2 different things – eyllanesc Dec 10 '18 at 21:43
  • So, you add items to the scene, but they get "magically" deleted shortly after? you don't call `QGraphicsScene::clear()` from somewhere by any chance, do you? In any case, something in your code causes the scene to be cleared. – hyde Dec 10 '18 at 21:51

1 Answers1

0

Thanks to @hyde , I found the answer: I executed scene->clear() in a slot called after loading some pictures. Apparently, the execution was delayed by Qt. (Some debug output was output before the scene was deleted in reality despite these output instructions were placed after the call of QGraphicsView::clear() in the source code.)

user7427029
  • 255
  • 2
  • 12