I have created a Qt Widgets Application using Qt Creator(Windows 7, MinGW).
I have added a GraphicsView (named as graphicsView
) and a push button (named as pbClick
).
The on_pbClick_clicked()
function is given below:
void MainWindow::on_pbClick_clicked()
{
QGraphicsScene scene;
//adding some text to the scene
scene.addText("Hello, world!", QFont("Times", 20, QFont::Bold));
ui->graphicsView->setScene(&scene);
ui->graphicsView->show();
}
When I click the pbClick
button, nothing happens within the graphicsView
.
How can I make the "Hello, world!" text be shown inside the graphicsView
.
You create your scene on stack, it is a problem, try to create it on heap (use pointers) in this case if there are not any mistakes, all should works fine. As doc said:
The view does not take ownership of scene.
http://qt-project.org/doc/qt-5/qgraphicsview.html#setScene
It means that when you create scene on stack, this scene will be deleted "in the end" of on_pbClick_clicked
slot. So your scene does not exist anymore, and you can't see nothing.
QGraphicsScene *scene = new QGraphicsScene;
//adding some text to the scene
scene->addText("Hello, world!", QFont("Times", 20, QFont::Bold));
ui->graphicsView->setScene(scene);
ui->graphicsView->show();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.