当我在 Qt 中调整窗口大小时,程序在 QLabel::setPixmap() 崩溃

问题描述

我选择了一个 QLabel 来显示 Basler 相机的实时图像。我使用线程循环来捕获图像并调用回调将图像发送到客户端。主要代码如下:

void BaslerCamera::captureProcess(void*)
{
    for (; ;)
    {
        if (m_captureThread.isExiting()) break;

        try
        {
            std::lock_guard<std::mutex> lock{m_capMutex};
            m_liveGrabResult.Release();
            if (m_camera.GrabOne(1000,m_liveGrabResult))
            {
                Image image;
                image.height = m_liveGrabResult->GetHeight();
                image.width = m_liveGrabResult->GetWidth();
                image.data = (uint8_t*)m_liveGrabResult->GetBuffer();

                fireImageCapturedCallback(image);
            }
        }
        catch (const GenICam::GenericException& e)
        {
            AERROR << e.GetDescription();
            break;
        }
    }
}

void fireImageCapturedCallback(const Image& img)
{
    for (const auto& callback : m_imageCapCallbacks)
    {
        callback(img);
    }
}

void CameraTuningWidget::onImageCaptured(const Image& img)
{
    //qDebug() << "Receive a live image,width: " << img.width << " height: " << img.height;
    QImage qImage(img.data,img.width,img.height,QImage::Format_Indexed8);
    qImage.scaled(ui->labelLiveImage->size(),Qt::KeepAspectRatio);
    ui->labelLiveImage->setpixmap(Qpixmap::fromImage(qImage));
}

上面的代码效果很好,只是当我调整窗口大小时,例如最大化和恢复操作时,程序很有可能会触发异常并崩溃。例外情况如下:

Crash picture

我确信在这函数过程中图像数据缓冲区是有效的。如果我评论 setpixmap() 代码,这个崩溃永远不会发生。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)