Basically, calling QGraphicsScene::addPixmap() will return a QGraphicsPixmapItem. So all you have to do is to hold the QGraphicsPixmapItem pointer when adding a pixmap. When adding another pixmap, delete the previous QGraphicsPixmapItem first.
void MainWindow::display_image()
{
...
lpixmap = QPixmap::fromImage(limage
);
if(myPixmapItem)
delete myPixmapItem;
myPixmapItem= left_cam_view->addPixmap(lpixmap);
}
void MainWindow::display_image()
{
...
lpixmap = QPixmap::fromImage(limage);
if(myPixmapItem)
delete myPixmapItem;
myPixmapItem= left_cam_view->addPixmap(lpixmap);
}
To copy to clipboard, switch view to plain text mode
the myPixmapItem is a member of MainWindow, a pointer to QGraphicsPixmapItem. Remember to set it to 0 when constructing a MainWindow Object.
Another way is,
void MainWindow::init()
{
...
left_cam_view->addItem(myPixmapItem);
...
}
void MainWindow::display_image()
{
...
myPixmapItem->setPixmap(lpixmap);
...
}
void MainWindow::init()
{
...
myPixmapItem = new QGraphicsPixmapItem();
left_cam_view->addItem(myPixmapItem);
...
}
void MainWindow::display_image()
{
...
QPixmap lpixmap = QPixmap::fromImage(image);
myPixmapItem->setPixmap(lpixmap);
...
}
To copy to clipboard, switch view to plain text mode
In this way, you will only have a QGraphicsPixmapItem and no more creating and deleting. This one would be better solution. I'm sure calling QGraphicsPixmapItem::setPixmap() will clear the previous QPixmap.
Some other things are that you're creating a QImage and a QPixmap in the stack every time the display_image() is called. This will cause constructing and destructing them. I think it's better to make them members of the MainWindow. And then you don't have to construct and destruct them. It shouldn't take too much memory, since QPixmap is implicit sharing.
Bookmarks