0

I have a QLabel containing an image that is being grabbed from an attached camera.

I have a grabbing thread running that retrieves the latest image, and then calls setPixmap(), but the GUI often just stops updating.

The code in question:

QLabel *imageLabel;
QLabel *infoLabel;
...
imageLabel = new QLabel();
imageLabel->setFrameStyle(QFrame::StyledPanel | QFrame::Raised);
imageLabel->setAlignment(Qt::AlignCenter);

imageLabel->setFixedHeight(720);
imageLabel->setFixedWidth(1280);
...

void QtWindow::grabbing_thread()
{
    while (m_isRunning)
    {
        {
            std::lock_guard<std::mutex> guard(m_updatingImage);
            auto dispImage = padRunner.getDisplayImage();
            
            if (dispImage.channels() == 3)
                imageLabel->setPixmap(QPixmap::fromImage(QImage(dispImage.data, dispImage.cols, dispImage.rows, dispImage.step, QImage::Format_RGB888)));
            else
                imageLabel->setPixmap(QPixmap::fromImage(QImage(dispImage.data, dispImage.cols, dispImage.rows, dispImage.step, QImage::Format_RGBA8888)));

            imageLabel->update();
        }

        infoLabelText  = date::format("%T", std::chrono::system_clock::now()) + "\n";
        infoLabel->update();
        using namespace std::chrono_literals;
        std::this_thread::sleep_for(15ms);
    }
}

This works some of the time, and then it just freezes. But if I take my whole window, and literally move it around a bit, it restarts the display, and shows the latest frames again. I have no idea what to do to properly keep running (the camera is definitely running, the grabbing thread keeps acquiring images, the QtWindow just does not update its display).

Note: dispImage is a cv::Mat

SinisterMJ
  • 3,425
  • 2
  • 33
  • 53
  • In Qt the update of the GUI is supposed to happen in the main GUI thread and for it to update you need to end your function so it gets back to the event loop. Related: [https://stackoverflow.com/questions/14545961/modify-qt-gui-from-background-worker-thread](https://stackoverflow.com/questions/14545961/modify-qt-gui-from-background-worker-thread) – drescherjm Aug 30 '21 at 15:43
  • Oh, so I run in my grabbing thread quite normally, but at the end when I have new text and image, I trigger some event in the class to set the Pixmap and Label there? – SinisterMJ Aug 30 '21 at 15:53
  • I think that will likely be the way. – drescherjm Aug 30 '21 at 15:55

0 Answers0