在这个问题中,我终于明白了使用低版本的好处,在网上大多是qt5版本的教程,所以推荐大家使用qt5,尤其是在这个问题中,尤为突出,在qt6中大量修改了关于摄像头调用的方法,但是如果你会了这套逻辑,同样你也可以按这个逻辑写qt6
//在mainwindow.cpp中的方法中添加
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
//ui->setupUi(this)必须是第一行,否则程序崩溃
ui->setupUi(this);
camera = new QCamera();
QList<QCameraInfo> cameras=QCameraInfo::availableCameras();
qDebug()<<cameras.size();
cameraViewFinder = new QCameraViewfinder();
ui->verticalLayout->addWidget(cameraViewFinder);
camera->setViewfinder(cameraViewFinder);
}