Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Refresh QGraphicsView
Qt 6.11 is out! See what's new in the release blog

Refresh QGraphicsView

Scheduled Pinned Locked Moved General and Desktop
8 Posts 4 Posters 8.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jk_mk
    wrote on last edited by
    #1

    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 ;
    }@

    1 Reply Last reply
    0
    • S Offline
      S Offline
      Satmosc
      wrote on last edited by
      #2

      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.

      M.Hatami
      To the Rationalism

      1 Reply Last reply
      0
      • J Offline
        J Offline
        jk_mk
        wrote on last edited by
        #3

        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?

        1 Reply Last reply
        0
        • B Offline
          B Offline
          baysmith
          wrote on last edited by
          #4

          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;
          

          }
          @

          Nokia Certified Qt Specialist.

          1 Reply Last reply
          0
          • J Offline
            J Offline
            jk_mk
            wrote on last edited by
            #5

            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 ;

            }@

            1 Reply Last reply
            0
            • B Offline
              B Offline
              baysmith
              wrote on last edited by
              #6

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

              Nokia Certified Qt Specialist.

              1 Reply Last reply
              0
              • J Offline
                J Offline
                jk_mk
                wrote on last edited by
                #7

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

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  Aniket
                  wrote on last edited by
                  #8

                  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.

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved