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. Add QImage to QLabel
Forum Updated to NodeBB v4.3 + New Features

Add QImage to QLabel

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 4 Posters 260 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.
  • I Offline
    I Offline
    itzwich1
    wrote on last edited by
    #1

    Hey guys, I have the following problem. I receive a QVideoFrame from my camera. I convert this to a Qimage and save it as a QPixmap in a label. However, this does not display the image. But when I load the pixmap from the label and save it locally, I can see the image. If I save the image locally before writing it to the label, reload it, and then insert the loaded image into the label, it works.

    Class where I get the QVideoFrame. This function is always called when a new camera image is available.

    connect( m_io->getCamera( ), &Camera::imageAvailable, this, &Measurement::newImageAvailable );
    
    void Measurement::newImageAvailable( )
    {
      static QDateTime dt = QDateTime::currentDateTime( );
    
      if ( dt.msecsTo( QDateTime::currentDateTime( ) ) < 100 ) {
        return;
      }
    
      auto frame = m_io->getCamera( )->getVideoFrame( );
      dt = QDateTime::currentDateTime( );
      QImage img = frame.toImage( );
    
      camDialog.updateLable( QPixmap::fromImage( frame.toImage( ) ) );
    }
    
    void CameraDialog::updateLable( QPixmap pixMap )
    {
      ui->label->setPixmap(pixMap);
    }
    

    The size of the label should also fit.
    I hope anybody can help.

    Christian EhrlicherC 1 Reply Last reply
    0
    • I itzwich1

      Hey guys, I have the following problem. I receive a QVideoFrame from my camera. I convert this to a Qimage and save it as a QPixmap in a label. However, this does not display the image. But when I load the pixmap from the label and save it locally, I can see the image. If I save the image locally before writing it to the label, reload it, and then insert the loaded image into the label, it works.

      Class where I get the QVideoFrame. This function is always called when a new camera image is available.

      connect( m_io->getCamera( ), &Camera::imageAvailable, this, &Measurement::newImageAvailable );
      
      void Measurement::newImageAvailable( )
      {
        static QDateTime dt = QDateTime::currentDateTime( );
      
        if ( dt.msecsTo( QDateTime::currentDateTime( ) ) < 100 ) {
          return;
        }
      
        auto frame = m_io->getCamera( )->getVideoFrame( );
        dt = QDateTime::currentDateTime( );
        QImage img = frame.toImage( );
      
        camDialog.updateLable( QPixmap::fromImage( frame.toImage( ) ) );
      }
      
      void CameraDialog::updateLable( QPixmap pixMap )
      {
        ui->label->setPixmap(pixMap);
      }
      

      The size of the label should also fit.
      I hope anybody can help.

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

      @itzwich1 said in Add QImage to QLabel:

      camDialog.updateLable

      Does your measurement class really have the dialog to show the image as class member? This is bad design as they should not know each other. Pass the image through a signal/slot connection.

      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
      0
      • I Offline
        I Offline
        itzwich1
        wrote on last edited by
        #3

        You're right. But I have to call up the dialog within my measurement class. Even if I change that, it doesn't solve my original problem.

        Christian EhrlicherC 1 Reply Last reply
        0
        • I itzwich1

          You're right. But I have to call up the dialog within my measurement class. Even if I change that, it doesn't solve my original problem.

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

          @itzwich1 said in Add QImage to QLabel:

          Even if I change that, it doesn't solve my original problem.

          How? Please provide a minimal compileable example of your problem. I would guess you have two instances of your dialog.

          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
          0
          • B Offline
            B Offline
            Bonnie
            wrote on last edited by Bonnie
            #5

            You can print some information. Have you confirmed that everytime frame has data and img is valid?
            I guess maybe some null images are set after some valid ones.

            I 1 Reply Last reply
            0
            • B Bonnie

              You can print some information. Have you confirmed that everytime frame has data and img is valid?
              I guess maybe some null images are set after some valid ones.

              I Offline
              I Offline
              itzwich1
              wrote on last edited by
              #6

              @Bonnie said in Add QImage to QLabel:

              You can print some information. Have you confirmed that everytime frame has data and img is valid?
              I guess maybe some null images are set after some valid ones.

              The image should be valid. frame.isValid always returns true and QImage.isNull always returns false. Why would it be bad if an image were empty? It shouldn't matter, right?

              JonBJ 1 Reply Last reply
              0
              • I itzwich1

                @Bonnie said in Add QImage to QLabel:

                You can print some information. Have you confirmed that everytime frame has data and img is valid?
                I guess maybe some null images are set after some valid ones.

                The image should be valid. frame.isValid always returns true and QImage.isNull always returns false. Why would it be bad if an image were empty? It shouldn't matter, right?

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by
                #7

                @itzwich1
                If a frame image were empty it would be fine but you wouldn't see anything, which might be what you are reporting.

                I don't think anyone can guess from what you say. If it's not that, for all we know you might have a different dialog or a different label from what you think you are working with. There is so much you can try to help you debug. Make sure you understand how many times your slot is being called. Save the image to file from newImageAvailable( ) not from the label. Debug out information about the image. Load a saved, good image in newImageAvailable( ) instead of reading it from video and send that to updateLable(). Are you re-entering the Qt event loop after you have changed the label, else you won't see it?

                I know nothing about this area. But I don't see any Qt method getVideoFrame( ). I don't know whether you ought be looking at QMediaCaptureSession Class for this kind of capture.

                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