我正在制作一个 Qt GUI 应用程序,它使用自定义QLabel
类(名称为ImageInteraction
)来显示来自流式摄像机的图像,同时还允许鼠标在图像上交互。由于 GUI 具有其他功能,定制的 QLabel 类完成从相机中提取图像并通过while
在另一个线程中运行的函数中的循环更新其显示的图像的工作。代码如下:
void ImageInteraction::startVideo()
{
if (!capture.open(streamUrl))
{
QMessageBox::warning(this, "Error", "No input device availabe!");
}
else
{
QFuture<void> multiprocess = QtConcurrent::run(this, &ImageInteraction::loadVideo);
}
}
void ImageInteraction::loadVideo()
{
while(loopContinue){
cv::Mat frame;
capture.read(frame);
if(!frame.empty())
{
cv::cvtColor(frame, frame, CV_BGR2RGBA);
cv::resize(frame, frame, cv::Size(this->width(), this->height()), 0, 0);
QImage image(frame.data, frame.cols, frame.rows, frame.step, QImage::Format_RGBA8888);
this->setPixmap(QPixmap::fromImage(image));
}
}
capture.release();
}
Here capture
is of typecv::VideoCapture
并且loopContinue
是一个布尔类型,最初设置为true
. 有一个closeEvent()
函数调用停止从相机捕获图像的方法。
void MainWindow::closeEvent(QCloseEvent *event)
{
liveVideo->stopVideoThread();//liveVideo is a pointer to an object of ImageInteraction
event->accept();
}
其中stopVideoThread
只需将布尔标志设置loopContinue
为false
并具有以下简单代码:
void ImageInteraction::stopVideoThread()
{
mutex.lock();//QMutex mutex;
loopContinue = false;
mutex.unlock();
}
据我了解,一旦方法被调用并设置为 false while
,方法中的循环loadVideo
应该停止。但实际上,当按下关闭按钮时,显然它不会停止循环并且应用程序崩溃并显示一条消息:stopVideoThread
loopContinue
while
The inferior stopped because it received a signal from the operating system.
Signal name : SIGSEGV
Signal meaning : Segmentation fault
我是否错误地使用QtConcurrent::run
了方法和QMutex
对象?你能确定问题是什么吗?仅供参考,操作系统是 ubuntu 14.04,IDE 是 QtCreator。
谢谢!