Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/135.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在Qgraphicscene上绘画,而不丢失已在其上的图片。Qt C++;_C++_Qt_Qgraphicsview - Fatal编程技术网

C++ 在Qgraphicscene上绘画,而不丢失已在其上的图片。Qt C++;

C++ 在Qgraphicscene上绘画,而不丢失已在其上的图片。Qt C++;,c++,qt,qgraphicsview,C++,Qt,Qgraphicsview,正如问题所说,我的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

正如问题所说,我的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(无符号整数i=0;i秒;
y=pix_iter->first;
场景->加法器(x,y,1,1,pen,QBrush(Qt::SolidPattern));
pix_iter++;
}
ui->graphicsView->setScene(场景);
无论如何,我不知道该怎么做,只是更新场景以添加点,而不是设置一个新的点


任何帮助都将不胜感激。

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

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

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


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

只要您使用相同的场景指针操作,您就可以随意多次调用setScene(尽管它不会改变任何内容)。因此,首先要了解的是,QGraphicscene实际上是QGraphicsView的一个模型,所以您要么全局声明它(取决于应用程序设计),要么使用scene()方法如前一个答案所建议的


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

同意。场景将在其视图中显示其子对象的更改同意。场景将在其视图中显示其子对象的更改
    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);