Refresh QGraphicsView
-
Hello,
I am using a QGraphicsView and a button, in order to load and display images as seems from the following code. But when I have already loaded an image in my QGraphicsView, and I am trying to load an other image, then my new image is displayed on my previous image. What should I change to refresh my QGraphicsView correctly? graphicsView_inputImage is the name of my QGraphicsView and scene is a global pointer to my QGraphicsScene.
@ void MainWindow::push_button_File(void)
{QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"), QDir::currentPath()); if (!fileName.isEmpty()) { scene->addPixmap(QPixmap(fileName, 0, Qt::AutoColor)); ui->graphicsView_inputImage->setScene(scene); }return ;
}@ -
Addpixmap is actually a part of QGraphicsPixmapItem , so try to use set pixmap http://doc.qt.nokia.com/latest/qgraphicspixmapitem.html
As it is clear in your code , you are adding new PixMAP Item to the item and not setting new or removing old one.
-
Perhaps something like this.
@
void MainWindow::push_button_File(void)
{
static QGraphicsPixmapItem* pixmapItem;
QString fileName = QFileDialog::getOpenFileName(this,
tr("Open File"), QDir::currentPath());
if (!fileName.isEmpty()) {
if (!pixmapItem) {
pixmapItem = scene->addPixmap(QPixmap(fileName, 0, Qt::AutoColor));
ui->graphicsView_inputImage->setScene(scene);
} else {
pixmapItem->setPixmap(QPixmap(fileName, 0, Qt::AutoColor));
}
}return;}
@ -
I have tried this and I couldn't display my image in the QGraphicsView at all:
@void MainWindow::push_button_File(void)
{
QGraphicsPixmapItem* pixmapItem = new QGraphicsPixmapItem();
QString fileName = QFileDialog::getOpenFileName(this,
tr("Open File"), QDir::currentPath());
if (!fileName.isEmpty()) {
if (!pixmapItem) {
pixmapItem = scene->addPixmap(QPixmap(fileName, 0, Qt::AutoColor));
ui->graphicsView_inputImage->setScene(scene);
} else {
pixmapItem->setPixmap(QPixmap(fileName, 0, Qt::AutoColor));
}
}return;}@
and also I have tried this, but I took the same results (the new image on the previous image)
@void MainWindow::push_button_File(void)
{QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"), QDir::currentPath()); if (!fileName.isEmpty()) {QGraphicsPixmapItem *pix= new QGraphicsPixmapItem();
pix->setPixmap(QPixmap(fileName, 0, Qt::AutoColor));
scene->addItem(pix);
ui->graphicsView_inputImage->setScene(scene);
}return ;
}@