在主线程中传递一个摄像头名称给子线程,让子线程来实现摄像头的开启,关闭,拍照,录像,(点击对应的按钮,主线程发送信号给子线程),子线程四个函数,现在有两个摄像头来工作,让它们处在两个线程,
void MainWindow::init()
{
ui->start1->setEnabled(false);
ui->start2->setEnabled(false);
ui->close1->setEnabled(false);
ui->close2->setEnabled(false);
ui->pic1->setEnabled(false);
ui->pic2->setEnabled(false);
ui->video1->setEnabled(false);
ui->video2->setEnabled(false);
myThread *t1 = new myThread;
myThread *t2 = new myThread;
QThread *my1 = new QThread;
QThread *my2 = new QThread;
t1->moveToThread(my1);
t2->moveToThread(my2);
my1->start();
my2->start();
// connect(this,&MainWindow::setCamera,t1,&myThread::work);
// connect(this,&MainWindow::setCamera,t2,&myThread::work);
//获取可用摄像头列表
m_cameras = QMediaDevices::videoInputs();
for(const auto &camera : m_cameras)
{
ui->comboBox1->addItem(camera.description());
}
//设置1
connect(ui->setcamera1,&QPushButton::clicked,this,[=](){
QString text = ui->comboBox1->currentText();
for(const auto &cameraDevice : m_cameras)
{
//查找具体哪个摄像头
if(cameraDevice.description() == text)
{
ui->label->clear();
ui->label->setText(cameraDevice.description());
emit setCamera(cameraDevice);
bool b = connect(this,&MainWindow::setCamera,t1,&myThread::work);
qDebug()<<b;
}
}
});
//设置2
connect(ui->setcamera2,&QPushButton::clicked,this,[=](){
QString text = ui->comboBox1->currentText();
for(const auto &cameraDevice : m_cameras)
{
//查找具体哪个摄像头
if(cameraDevice.description() == text)
{
ui->label_2->clear();
ui->label_2->setText(cameraDevice.description());
ui->start2->setEnabled(true);
emit setCamera(cameraDevice);
// bool b = connect(this,&MainWindow::setCamera,t2,&myThread::work);
// qDebug()<<b;
}
}
});
麻烦各位帅哥美女看一看