我有我的主线程负责GUI和我的工作线程负责处理我的网络摄像头。
我可以从主线程向我的网络摄像头线程发出信号,但它从未接收到它!有人可以解释一下原因吗?
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
setThreadsAndObj();
setConnects();
startCamera();
}
void MainWindow::setThreadsAndObj()
{
cameraClassPtr = new cameraClass();
cameraThread = new QThread();
}
void MainWindow::setConnects()
{
....
connect(this, &MainWindow::savePicture, cameraClassPtr, &cameraClass::takePicture);
}
void MainWindow::startCamera()
{
//camera start
connect( cameraThread, &QThread::started, cameraClassPtr, &cameraClass::getVideoFrame );
connect( cameraThread, &QThread::finished, cameraClassPtr, &QThread::deleteLater );
connect( cameraThread, &QThread::finished, cameraThread, &QThread::deleteLater );
//receive camera frames
connect(cameraClassPtr, &cameraClass::videoFrameToGui, this, &MainWindow::updateCameraStream);
//start threads
cameraClassPtr->moveToThread(cameraThread);
cameraThread->start();
}
void MainWindow::foo()
{
emit savePicture();
}
void cameraClass::takePicture()
{
qDebug()<<"camera class received signal"; //THIS NEVER GETS PRINTED
}
有人可以解释一下为什么我的信号从未收到过,而且“相机类接收信号”从未被打印过以及如何解决这个问题?
答案 0 :(得分:0)
我在工作线程中有这个循环:
...
static cv::VideoCapture cap(-1);
if( !cap.isOpened() )
{
qDebug()<< "Could not initialize capturing...\n";
}
qDebug()<<"starting live camera";
cap.set(CV_CAP_PROP_FRAME_WIDTH,240);
cap.set(CV_CAP_PROP_FRAME_HEIGHT,320);
while(1)
{
cap >> imgFrame;
int fd = open("/home/John/Desktop/mainImglock.txt", O_RDWR |O_CREAT, 0666);
int rc = flock(fd, LOCK_EX | LOCK_NB);
if(rc==0)
{
cv::imwrite("/home/John/Desktop/camera.jpg", imgFrame);
}
flock(fd, LOCK_UN);
close(fd);
}
我通过在while(1)循环中添加:QApplication::processEvents();
解决了这个问题,因此事件循环不会挂起。
答案 1 :(得分:0)
你需要继承QThread并调用QThread :: exec();为了处理这个帖子中的事件。