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. How to show image on QGraphicsView
Forum Updated to NodeBB v4.3 + New Features

How to show image on QGraphicsView

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 3 Posters 2.2k 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 have opencv Mat image and want to show it on QGraphicsView. According to I read from internet, I did:
    header:

    QGraphicsScene *scene = new QGraphicsScene;
    

    source(drawing slot):

            cv::cvtColor(img, img, CV_BGR2RGB);
        
            QPixmap pixmap(QPixmap::fromImage(QImage(img.data, img.cols, img.rows, img.step, QImage::Format_RGB888)));
            scene->addPixmap(pixmap);
            ui->graphicsView->setScene(scene);
        
    

    When a signal emitted, this slot runs. When I run program, after emitting the signal the program crash after immediately showing image on graphicsview. What can be possible issue? If there is much better ui element for images, I can use it also.

    JonBJ 1 Reply Last reply
    0
    • M masa4

      I have opencv Mat image and want to show it on QGraphicsView. According to I read from internet, I did:
      header:

      QGraphicsScene *scene = new QGraphicsScene;
      

      source(drawing slot):

              cv::cvtColor(img, img, CV_BGR2RGB);
          
              QPixmap pixmap(QPixmap::fromImage(QImage(img.data, img.cols, img.rows, img.step, QImage::Format_RGB888)));
              scene->addPixmap(pixmap);
              ui->graphicsView->setScene(scene);
          
      

      When a signal emitted, this slot runs. When I run program, after emitting the signal the program crash after immediately showing image on graphicsview. What can be possible issue? If there is much better ui element for images, I can use it also.

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

      @masa4
      Check the return result of QPixmap::fromImage(QImage(img.data)) for validity. Run under debugger and show stack trace when the "crash" occurs.

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

        As always - a function is used but the documentation is not read.
        QImage ctor: "The buffer must remain valid throughout the life of the QImage and all copies that have not been modified or otherwise detached from the original buffer. The image does not delete the buffer at destruction. You can provide a function pointer cleanupFunction along with an extra pointer cleanupInfo that will be called when the last copy is destroyed."

        So make a deep copy of your QImage or make sure the buffer remains valid until the QImage gets destroyed.

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

        M 1 Reply Last reply
        3
        • Christian EhrlicherC Christian Ehrlicher

          As always - a function is used but the documentation is not read.
          QImage ctor: "The buffer must remain valid throughout the life of the QImage and all copies that have not been modified or otherwise detached from the original buffer. The image does not delete the buffer at destruction. You can provide a function pointer cleanupFunction along with an extra pointer cleanupInfo that will be called when the last copy is destroyed."

          So make a deep copy of your QImage or make sure the buffer remains valid until the QImage gets destroyed.

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

          @Christian-Ehrlicher So, How to do it?

          Christian EhrlicherC 1 Reply Last reply
          0
          • M masa4

            @Christian-Ehrlicher So, How to do it?

            Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @masa4 said in How to show image on QGraphicsView:

            So, How to do it?

            So make a deep copy of your QImage or make sure the buffer remains valid until the QImage gets destroyed.

            See e.g. QImage::copy()

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

            M 1 Reply Last reply
            2
            • Christian EhrlicherC Christian Ehrlicher

              @masa4 said in How to show image on QGraphicsView:

              So, How to do it?

              So make a deep copy of your QImage or make sure the buffer remains valid until the QImage gets destroyed.

              See e.g. QImage::copy()

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

              @Christian-Ehrlicher
              header:

              QImage *qimg;
              

              source:

              qimg = new QImage(img.data, img.cols, img.rows, img.step, QImage::Format_RGB888);
              QPixmap pixmap(QPixmap::fromImage(qimg->copy(0,0,img.cols, img.rows)));
              

              with this still not work.

              Christian EhrlicherC 1 Reply Last reply
              0
              • M masa4

                @Christian-Ehrlicher
                header:

                QImage *qimg;
                

                source:

                qimg = new QImage(img.data, img.cols, img.rows, img.step, QImage::Format_RGB888);
                QPixmap pixmap(QPixmap::fromImage(qimg->copy(0,0,img.cols, img.rows)));
                

                with this still not work.

                Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by Christian Ehrlicher
                #7

                QPixmap pixmap(QPixmap::fromImage(QImage(img.data, img.cols, img.rows, img.step, QImage::Format_RGB888)).copy());

                if this does not work than your incoming image data is not correct. You can easily look what's inside the QImage by saving it to a file.

                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
                1

                • Login

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