如何解决当我在 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));
}
上面的代码效果很好,只是当我调整窗口大小时,例如最大化和恢复操作时,程序很有可能会触发异常并崩溃。例外情况如下:
我确信在这个函数过程中图像数据缓冲区是有效的。如果我评论 setpixmap() 代码,这个崩溃永远不会发生。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。