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.
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.)