简体   繁体   English

如何在Qt窗口小部件项目(Qt Creator)中更新图形视图

[英]How to update Graphics View in a Qt Widgets Project(Qt Creator)

I have created a Qt Widgets Application using Qt Creator(Windows 7, MinGW). 我已经使用Qt Creator(Windows 7,MinGW)创建了Qt Widgets应用程序。

I have added a GraphicsView (named as graphicsView ) and a push button (named as pbClick ). 我添加了一个GraphicsView(命名为graphicsView )和一个按钮(命名为pbClick )。

The on_pbClick_clicked() function is given below: 下面给出了on_pbClick_clicked()函数:

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 . 当我单击pbClick按钮时, graphicsView内什么也没有发生。

How can I make the "Hello, world!" 我该如何制作“你好,世界!” text be shown inside the graphicsView . 文本将显示在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 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. 这意味着当您在堆栈上创建场景时,该场景将在on_pbClick_clicked插槽的“末尾”被删除。 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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM