Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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.



  • Thanks for your response. But the scene is member of QGraphicsScene and scene doesn't include the method setPixmap(). How could I set a new pixmap? Could you be more specific?



  • 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 ;

    }@



  • The first thing you tried is not what I suggested. Remove the "new QGraphicsPixmapItem()"



  • Ok that was my mistake. Now I am taking the correct results. Thanks a lot for your help!!



  • following Error Coming in my system..!
    QPixmap::operator=: Cannot assign to pixmap during painting.

    When i was trying to do this code with menubar actions.

    Please help me out.


Log in to reply