Reputation: 4607
SO as the question says, i have a QGraphicsView on my UI. I have made a function that puts an image onto that 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);
However now i want to be able to paint dots at specific X Y values on the image. i have a function that does this quite nicely however as soon as this function is called all the dots appear and the picture vanishes :(. i know that this is because im setting the scene again to the one with dots on so the program just gets rid of the one thats its currently using (the image one)
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);
anyway i dont know what to do to just update the scene to add dots to it instead of just setting a new one
any help would be really appreciated.
Upvotes: 0
Views: 1055
Reputation: 4085
As long as you operate with same scene pointer you can call setScene as many times as you like (although it doesn't change anything). So, first thing to understand is that QGraphicsScene is actually a model to QGraphicsView, so you either should declare it globally (depending on out application design) or use scene() method as being suggested to previous answer.
Calling scene() for a view is of course a solution but I prefer to keep model pointers available through my own classes rather then through view's methods. Thats because some times you want to share a view to show different models depending on application state/logic. So if something has to be added to a scene you should be sure which scene you are working with.
Upvotes: 2
Reputation: 13238
In the second snippet, do you re-create the scene? Why don't use ui->graphicsView->scene()
and add dots to it?
Upvotes: 3