代码如下
#-------------------------------------------------
#
# Project created by QtCreator 2020-01-13T10:46:35
#
#-------------------------------------------------QT += core gui
QT += multimedia
QT += multimediawidgetsgreaterThan(QT_MAJOR_VERSION, 4): QT += widgetsTARGET = camera
TEMPLATE = app# The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0SOURCES += main.cpp\widget.cppHEADERS += widget.hFORMS += widget.ui
#ifndef WIDGET_H
#define WIDGET_H#include
#include
#include
#include namespace Ui {
class Widget;
}class Widget : public QWidget
{Q_OBJECTpublic:explicit Widget(QWidget *parent = 0);~Widget();private slots:void on_openCamera_clicked();void on_closeCamera_clicked();private:Ui::Widget *ui;QCamera* m_camera; //读取摄像头QCameraViewfinder* m_cameraViewFinder; //渲染摄像头};#endif // WIDGET_H
#include "widget.h"
#include "ui_widget.h"Widget::Widget(QWidget *parent) :QWidget(parent),ui(new Ui::Widget)
{ui->setupUi(this);setWindowTitle("Camera");m_camera = new QCamera();m_cameraViewFinder = new QCameraViewfinder();
}Widget::~Widget()
{delete ui;
}void Widget::on_openCamera_clicked()
{ui->cameraShow->addWidget(m_cameraViewFinder); //为QHBoxLayoutm_camera->setViewfinder(m_cameraViewFinder);m_camera->start();
}void Widget::on_closeCamera_clicked()
{m_camera->stop();
}
其它部分省略