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. QGraphicsView changes on scene does not reflect
Forum Updated to NodeBB v4.3 + New Features

QGraphicsView changes on scene does not reflect

Scheduled Pinned Locked Moved Unsolved General and Desktop
15 Posts 3 Posters 1.0k Views
  • 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.
  • M Offline
    M Offline
    masa4
    wrote on last edited by
    #1

    I am using QGraphicsView for showing images. First my MainSlot runs and it does its job, But i have 2 other slots according to 2 button and they re not working.
    HEADER:

    class MyClass{
    private:
        QGraphicsScene *scene = new QGraphicsScene;
        QImage qimg;
        QImage scaledQimg;
        QPixmap pixmap;
        cv::Mat img;
    }
    

    SOURCE:

    void MainSlot(){
    img = cv::Mat(...);
    qimg = QImage(img.data, img.cols, img.rows, img.step, QImage::Format_Grayscale8);
    scaledQimg = qimg.scaled(ui->graphicsView->width() - 10, ui->graphicsView->height() - 10, Qt::KeepAspectRatio);
    pixmap = QPixmap::fromImage(scaledQimg);
    
    scene->addPixmap(pixmap);
    ui->graphicsView->setScene(scene);
    }
    
    void originalSize()
    {
        pixmap = QPixmap::fromImage(qimg);
        scene->update();
        ui->graphicsView->update();
    }
    
    void ScaledSize()
    {
        pixmap = QPixmap::fromImage(scaledQimg);
        scene->update();
        ui->graphicsView->update();
    }
    

    Whats wrong with my code?

    JonBJ 1 Reply Last reply
    0
    • M masa4

      I am using QGraphicsView for showing images. First my MainSlot runs and it does its job, But i have 2 other slots according to 2 button and they re not working.
      HEADER:

      class MyClass{
      private:
          QGraphicsScene *scene = new QGraphicsScene;
          QImage qimg;
          QImage scaledQimg;
          QPixmap pixmap;
          cv::Mat img;
      }
      

      SOURCE:

      void MainSlot(){
      img = cv::Mat(...);
      qimg = QImage(img.data, img.cols, img.rows, img.step, QImage::Format_Grayscale8);
      scaledQimg = qimg.scaled(ui->graphicsView->width() - 10, ui->graphicsView->height() - 10, Qt::KeepAspectRatio);
      pixmap = QPixmap::fromImage(scaledQimg);
      
      scene->addPixmap(pixmap);
      ui->graphicsView->setScene(scene);
      }
      
      void originalSize()
      {
          pixmap = QPixmap::fromImage(qimg);
          scene->update();
          ui->graphicsView->update();
      }
      
      void ScaledSize()
      {
          pixmap = QPixmap::fromImage(scaledQimg);
          scene->update();
          ui->graphicsView->update();
      }
      

      Whats wrong with my code?

      JonBJ Online
      JonBJ Online
      JonB
      wrote on last edited by
      #2

      @masa4 said in QGraphicsView changes on scene does not reflect:

      and they re not working.

      It really helps if you say what you mean by this. Are we supposed to guess?

      If my "guess" is "when I change pixmap in the 2 functions shown the pixmap shown on the graphics scene does not change", that would be because addPixmap(pixmap) takes a copy of pixmap, it does not make the scene have a dynamic reference to pixmap. Is that what you are asking about?

      M 1 Reply Last reply
      0
      • JonBJ JonB

        @masa4 said in QGraphicsView changes on scene does not reflect:

        and they re not working.

        It really helps if you say what you mean by this. Are we supposed to guess?

        If my "guess" is "when I change pixmap in the 2 functions shown the pixmap shown on the graphics scene does not change", that would be because addPixmap(pixmap) takes a copy of pixmap, it does not make the scene have a dynamic reference to pixmap. Is that what you are asking about?

        M Offline
        M Offline
        masa4
        wrote on last edited by
        #3

        @JonB Yeah basically nothing happens when i click these 2 buttons. So you mean I should make pixmap pointer? For example:

        void ScaledSize()
        {
            pixmap = QPixmap::fromImage(scaledQimg);
            scene->update();
            ui->graphicsView->update();
        }
        

        here i am creating a pixmap with QPixmap::fromImage(scaledQimg) and assigning it to my pixmap. Isnt it sufficient?

        jsulmJ JonBJ 2 Replies Last reply
        0
        • M masa4

          @JonB Yeah basically nothing happens when i click these 2 buttons. So you mean I should make pixmap pointer? For example:

          void ScaledSize()
          {
              pixmap = QPixmap::fromImage(scaledQimg);
              scene->update();
              ui->graphicsView->update();
          }
          

          here i am creating a pixmap with QPixmap::fromImage(scaledQimg) and assigning it to my pixmap. Isnt it sufficient?

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @masa4 said in QGraphicsView changes on scene does not reflect:

          assigning it to my pixmap. Isnt it sufficient?

          Of course not.
          Why should anything change in the scene if you update pixmap variable?

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          M 1 Reply Last reply
          0
          • M masa4

            @JonB Yeah basically nothing happens when i click these 2 buttons. So you mean I should make pixmap pointer? For example:

            void ScaledSize()
            {
                pixmap = QPixmap::fromImage(scaledQimg);
                scene->update();
                ui->graphicsView->update();
            }
            

            here i am creating a pixmap with QPixmap::fromImage(scaledQimg) and assigning it to my pixmap. Isnt it sufficient?

            JonBJ Online
            JonBJ Online
            JonB
            wrote on last edited by JonB
            #5

            @masa4
            You need to alter the QPixmap which was created on the scene via scene->addPixmap().
            Or, possibly, depending on your desires, remove it and put a new one instead.

            M 1 Reply Last reply
            1
            • jsulmJ jsulm

              @masa4 said in QGraphicsView changes on scene does not reflect:

              assigning it to my pixmap. Isnt it sufficient?

              Of course not.
              Why should anything change in the scene if you update pixmap variable?

              M Offline
              M Offline
              masa4
              wrote on last edited by
              #6

              @jsulm because i updating scene&graphicsview too?

              jsulmJ 1 Reply Last reply
              0
              • M masa4

                @jsulm because i updating scene&graphicsview too?

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @masa4 You need to understand that your pixmap variable has no connection to your scene. If you call scene->addPixmap(pixmap); your pixmap is copied into the scene, this was already pointed out by @JonB . If you change pixmap variable later it has zero influence on the scene. You need to update the pixmap item in the scene...

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                M 1 Reply Last reply
                3
                • JonBJ JonB

                  @masa4
                  You need to alter the QPixmap which was created on the scene via scene->addPixmap().
                  Or, possibly, depending on your desires, remove it and put a new one instead.

                  M Offline
                  M Offline
                  masa4
                  wrote on last edited by
                  #8

                  @JonB yeah now worker with this:

                  void originalSize()
                  {
                      pixmap = QPixmap::fromImage(qimg);
                      scene->clear();
                      scene->addPixmap(pixmap);
                      scene->update();
                      ui->graphicsView->update();
                  }
                  

                  But original size image not sitting at the center of graphicsview. You know any trick for centerize it?

                  jsulmJ JonBJ 2 Replies Last reply
                  0
                  • jsulmJ jsulm

                    @masa4 You need to understand that your pixmap variable has no connection to your scene. If you call scene->addPixmap(pixmap); your pixmap is copied into the scene, this was already pointed out by @JonB . If you change pixmap variable later it has zero influence on the scene. You need to update the pixmap item in the scene...

                    M Offline
                    M Offline
                    masa4
                    wrote on last edited by
                    #9

                    @jsulm Yep now i got the concept.

                    1 Reply Last reply
                    0
                    • M masa4

                      @JonB yeah now worker with this:

                      void originalSize()
                      {
                          pixmap = QPixmap::fromImage(qimg);
                          scene->clear();
                          scene->addPixmap(pixmap);
                          scene->update();
                          ui->graphicsView->update();
                      }
                      

                      But original size image not sitting at the center of graphicsview. You know any trick for centerize it?

                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @masa4 Now you are adding a new pixmap item all the time. I doubt this is what you want. What you should do instead: scene->addPixmap(pixmap); returns a pointer to QGraphicsPixmapItem, store this pointer as member variable and call https://doc.qt.io/qt-6/qgraphicspixmapitem.html#setPixmap on it every time you need to update pixmap.

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      M 1 Reply Last reply
                      2
                      • M masa4

                        @JonB yeah now worker with this:

                        void originalSize()
                        {
                            pixmap = QPixmap::fromImage(qimg);
                            scene->clear();
                            scene->addPixmap(pixmap);
                            scene->update();
                            ui->graphicsView->update();
                        }
                        

                        But original size image not sitting at the center of graphicsview. You know any trick for centerize it?

                        JonBJ Online
                        JonBJ Online
                        JonB
                        wrote on last edited by
                        #11

                        @masa4 said in QGraphicsView changes on scene does not reflect:

                        But original size image not sitting at the center of graphicsview. You know any trick for centerize it?

                        void QGraphicsView::centerOn(const QGraphicsItem *item)

                        1 Reply Last reply
                        2
                        • jsulmJ jsulm

                          @masa4 Now you are adding a new pixmap item all the time. I doubt this is what you want. What you should do instead: scene->addPixmap(pixmap); returns a pointer to QGraphicsPixmapItem, store this pointer as member variable and call https://doc.qt.io/qt-6/qgraphicspixmapitem.html#setPixmap on it every time you need to update pixmap.

                          M Offline
                          M Offline
                          masa4
                          wrote on last edited by
                          #12

                          @jsulm You mean this right:
                          header:

                          ...
                          QGraphicsPixmapItem *pix;
                          

                          source:

                          void MainSlot(){
                          ...
                          pix = scene->addPixmap(pixmap);
                          ui->graphicsView->setScene(scene);
                          }
                          void originalSize()
                          {
                              pixmap = QPixmap::fromImage(qimg);
                              pix->setPixmap(pixmap);
                              ui->graphicsView->centerOn(ui->graphicsView->x()/2,ui->graphicsView->y()/2); //doesnt work
                          }
                          
                          void ScaledSize()
                          {
                              pixmap = QPixmap::fromImage(scaledQimg);
                              pix->setPixmap(pixmap);
                          }
                          

                          In this way it still works. centerize doesnt work by the way @JonB

                          JonBJ 1 Reply Last reply
                          0
                          • M masa4

                            @jsulm You mean this right:
                            header:

                            ...
                            QGraphicsPixmapItem *pix;
                            

                            source:

                            void MainSlot(){
                            ...
                            pix = scene->addPixmap(pixmap);
                            ui->graphicsView->setScene(scene);
                            }
                            void originalSize()
                            {
                                pixmap = QPixmap::fromImage(qimg);
                                pix->setPixmap(pixmap);
                                ui->graphicsView->centerOn(ui->graphicsView->x()/2,ui->graphicsView->y()/2); //doesnt work
                            }
                            
                            void ScaledSize()
                            {
                                pixmap = QPixmap::fromImage(scaledQimg);
                                pix->setPixmap(pixmap);
                            }
                            

                            In this way it still works. centerize doesnt work by the way @JonB

                            JonBJ Online
                            JonBJ Online
                            JonB
                            wrote on last edited by JonB
                            #13

                            @masa4 said in QGraphicsView changes on scene does not reflect:

                            In this way it still works. centerize doesnt work by the way @JonB

                            I never had any problem with it. Are you claiming you want to report it fails to work??

                            ui->graphicsView->centerOn(ui->graphicsView->x()/2,ui->graphicsView->y()/2); //doesnt work

                            In this way it still works

                            What are we expected to understand from the line saying "doesn't work" and one saying "still works"?

                            For your actual code (a) it's not what I suggested to use and (b) I don't know what you expect it to do.

                            M JonBJ 2 Replies Last reply
                            1
                            • JonBJ JonB

                              @masa4 said in QGraphicsView changes on scene does not reflect:

                              In this way it still works. centerize doesnt work by the way @JonB

                              I never had any problem with it. Are you claiming you want to report it fails to work??

                              ui->graphicsView->centerOn(ui->graphicsView->x()/2,ui->graphicsView->y()/2); //doesnt work

                              In this way it still works

                              What are we expected to understand from the line saying "doesn't work" and one saying "still works"?

                              For your actual code (a) it's not what I suggested to use and (b) I don't know what you expect it to do.

                              M Offline
                              M Offline
                              masa4
                              wrote on last edited by
                              #14

                              @JonB Haha sorry for poor grammar. I mean my code work, functions work after switching to QGraphicsPixmapItem.

                              And i added

                              ui->graphicsView->centerOn(ui->graphicsView->x()/2,ui->graphicsView->y()/2); //doesnt work
                              

                              this line for centering the image in graphics view. But image still not centered.

                              1 Reply Last reply
                              0
                              • JonBJ JonB

                                @masa4 said in QGraphicsView changes on scene does not reflect:

                                In this way it still works. centerize doesnt work by the way @JonB

                                I never had any problem with it. Are you claiming you want to report it fails to work??

                                ui->graphicsView->centerOn(ui->graphicsView->x()/2,ui->graphicsView->y()/2); //doesnt work

                                In this way it still works

                                What are we expected to understand from the line saying "doesn't work" and one saying "still works"?

                                For your actual code (a) it's not what I suggested to use and (b) I don't know what you expect it to do.

                                JonBJ Online
                                JonBJ Online
                                JonB
                                wrote on last edited by JonB
                                #15

                                @masa4 said in QGraphicsView changes on scene does not reflect:

                                ui->graphicsView->centerOn(ui->graphicsView->x()/2,ui->graphicsView->y()/2);
                                But image still not centered.

                                @JonB said in QGraphicsView changes on scene does not reflect:

                                For your actual code (a) it's not what I suggested to use and (b) I don't know what you expect it to do.

                                I already wrote the above previously.

                                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