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. Show Qimage on graphicsView
Qt 6.11 is out! See what's new in the release blog

Show Qimage on graphicsView

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 4 Posters 3.4k 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
    jss193
    wrote on last edited by
    #1

    Hello, Im trying to show a QImage into qgraphicsView, but it never works, I tried the following code:

       QGraphicsScene *scene = new QGraphicsScene;
    
        QImage image("path/QtImage.jpg");
    
        QGraphicsPixmapItem item(QPixmap::fromImage(image));
    
        scene->addItem(&item);
    
        ui->graphicsView->setScene(scene);
    
    

    I fist create a scene,then trnasform the image into a Qpixmap item and add item to scene, then I set the scene into graphicsView. I can not see anything.

    Can you help me please?

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @jss193 said in Show Qimage on graphicsView:

      QGraphicsPixmapItem item(QPixmap::fromImage(image));

      You're creating the object on the stack so it will get deleted once the function exits...

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      2
      • J Offline
        J Offline
        jss193
        wrote on last edited by jss193
        #3

        I created an object, why is it deleted? Dont understand well what you mean

        Thank u

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          Here's an example:

          void MyWidget::myFunction()
          {
              QImage image("path/QtImage.jpg");
              QGraphicsPixmapItem item(QPixmap::fromImage(image));
          } // << Here both image and item cease to exist. 
          

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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

            Thank you, I know that but what I showed is not part of a function, is part of pushing a button, these are the actions inside that code, but it remanins not showing any image.

            Thank you

            SGaistS 1 Reply Last reply
            0
            • A Offline
              A Offline
              Andy M
              wrote on last edited by
              #6

              They are saying you should allocate the QGraphicsPixmapItem on the heap:

              QGraphicsPixmapItem *item = new QGraphicsPixmapItem(QPixmap::fromImage(image));
              

              The scene takes ownership of it:

              https://doc.qt.io/qt-5/qgraphicsscene.html#addItem

              1 Reply Last reply
              2
              • J jss193

                Thank you, I know that but what I showed is not part of a function, is part of pushing a button, these are the actions inside that code, but it remanins not showing any image.

                Thank you

                SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @jss193 said in Show Qimage on graphicsView:

                Thank you, I know that but what I showed is not part of a function, is part of pushing a button, these are the actions inside that code, but it remanins not showing any image.

                Thank you

                A slot is still a function and the same logic applies to it as my shorten example.

                Do as @Andy-M wrote and allocate the item on the heap.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                2

                • Login

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