繁体   English   中英

在QGraphicsScene上绘画而不会丢失已经在其上的图片。 Qt C ++

[英]Painting on a QGraphicsScene without loosing the picture already on it. Qt C++

因此,正如问题所说,我的UI上有一个QGraphicsView。 我做了一个函数,将图像放到该QGraphicsView上:

// Read New Image from file
QImage image(":/images/myFile.png");

/// Declare a pointer to a scene
QGraphicsScene *scene = new QGraphicsScene();

// add a pixmap to the scene from the QImage 'image'
scene->addPixmap(QPixmap::fromImage(image));

// set the scene to equal the height and width of the map image
scene->setSceneRect(0,0,image.width(),image.height());

// Set the Scene to the GraphicsView on the UI
ui->graphicsView->setScene(scene);

但是现在我希望能够在图像上以特定的XY值绘制点。 我有一个功能很好的函数,但是一旦调用此函数,所有的点就会出现并且图片消失:(。我知道这是因为我将场景再次设置为带点的那个,所以程序只得到了摆脱了它当前正在使用的那个(图一)

    for(unsigned int i = 0; i < pixels.size(); i++)
    {
        x = pix_iter->second;
        y = pix_iter->first;

        scene->addEllipse(x, y, 1, 1, pen, QBrush(Qt::SolidPattern));

        pix_iter++;
    }
    ui->graphicsView->setScene(scene);

无论如何,我不知道该怎么做只是更新场景以向其添加点,而不是仅仅设置一个新的场景

任何帮助将非常感激。

在第二个片段中,您是否重新创建场景? 为什么不使用ui->graphicsView->scene()并在其中添加点?

只要您使用相同的场景指针进行操作,就可以随意调用setScene(尽管它不会改变任何内容)。 因此,首先要了解的是QGraphicsScene实际上是QGraphicsView的模型 ,因此您应该全局地声明它(取决于外部应用程序设计),或者根据先前答案的建议使用scene()方法。

为视图调用scene()当然是一个解决方案,但是我更喜欢通过我自己的类(而不是通过视图的方法)保持模型指针可用。 那是因为有时您希望根据应用程序状态/逻辑共享视图以显示不同的模型。 因此,如果必须将某些内容添加到场景中,则应确保正在使用哪个场景。

暂无
暂无

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

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