summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFriedemann Kleint <Friedemann.Kleint@qt.io>2017-06-23 15:18:22 +0200
committerFriedemann Kleint <Friedemann.Kleint@qt.io>2017-06-26 13:26:53 +0000
commite8e9f40ebc0d822fe639bed75d185800ff16b452 (patch)
treebd1123b5c358b39b18b28b69b3c0fa2d8eb3b528
parent019dc4d39ab87966d6f3e1c8f1d941a6d113f44b (diff)
downloadqtmultimedia-e8e9f40ebc0d822fe639bed75d185800ff16b452.tar.gz
Camera example: Brush up to C++11
Use Qt 5 signal/slot syntax, range based for, member initialization. Add message for captured images and fix some minor issues. Task-number: QTBUG-60627 Change-Id: I1011fc95d928a9d2edaad16120acb0dc41f987d9 Reviewed-by: Christian Stromme <christian.stromme@qt.io>
-rw-r--r--examples/multimediawidgets/camera/camera.cpp51
-rw-r--r--examples/multimediawidgets/camera/camera.h12
-rw-r--r--examples/multimediawidgets/camera/imagesettings.cpp9
-rw-r--r--examples/multimediawidgets/camera/imagesettings.h2
-rw-r--r--examples/multimediawidgets/camera/imagesettings.ui2
-rw-r--r--examples/multimediawidgets/camera/videosettings.cpp28
-rw-r--r--examples/multimediawidgets/camera/videosettings.h2
-rw-r--r--examples/multimediawidgets/camera/videosettings.ui2
8 files changed, 55 insertions, 53 deletions
diff --git a/examples/multimediawidgets/camera/camera.cpp b/examples/multimediawidgets/camera/camera.cpp
index 2a1af875d..7a9955b01 100644
--- a/examples/multimediawidgets/camera/camera.cpp
+++ b/examples/multimediawidgets/camera/camera.cpp
@@ -58,12 +58,7 @@ Q_DECLARE_METATYPE(QCameraInfo)
Camera::Camera(QWidget *parent) :
QMainWindow(parent),
- ui(new Ui::Camera),
- camera(0),
- imageCapture(0),
- mediaRecorder(0),
- isCapturingImage(false),
- applicationExiting(false)
+ ui(new Ui::Camera)
{
ui->setupUi(this);
@@ -71,7 +66,8 @@ Camera::Camera(QWidget *parent) :
QActionGroup *videoDevicesGroup = new QActionGroup(this);
videoDevicesGroup->setExclusive(true);
- foreach (const QCameraInfo &cameraInfo, QCameraInfo::availableCameras()) {
+ const QList<QCameraInfo> availableCameras = QCameraInfo::availableCameras();
+ for (const QCameraInfo &cameraInfo : availableCameras) {
QAction *videoDeviceAction = new QAction(cameraInfo.description(), videoDevicesGroup);
videoDeviceAction->setCheckable(true);
videoDeviceAction->setData(QVariant::fromValue(cameraInfo));
@@ -81,8 +77,8 @@ Camera::Camera(QWidget *parent) :
ui->menuDevices->addAction(videoDeviceAction);
}
- connect(videoDevicesGroup, SIGNAL(triggered(QAction*)), SLOT(updateCameraDevice(QAction*)));
- connect(ui->captureWidget, SIGNAL(currentChanged(int)), SLOT(updateCaptureMode()));
+ connect(videoDevicesGroup, &QActionGroup::triggered, this, &Camera::updateCameraDevice);
+ connect(ui->captureWidget, &QTabWidget::currentChanged, this, &Camera::updateCaptureMode);
setCamera(QCameraInfo::defaultCamera());
}
@@ -102,20 +98,21 @@ void Camera::setCamera(const QCameraInfo &cameraInfo)
camera = new QCamera(cameraInfo);
- connect(camera, SIGNAL(stateChanged(QCamera::State)), this, SLOT(updateCameraState(QCamera::State)));
- connect(camera, SIGNAL(error(QCamera::Error)), this, SLOT(displayCameraError()));
+ connect(camera, &QCamera::stateChanged, this, &Camera::updateCameraState);
+ connect(camera, QOverload<QCamera::Error>::of(&QCamera::error), this, &Camera::displayCameraError);
mediaRecorder = new QMediaRecorder(camera);
- connect(mediaRecorder, SIGNAL(stateChanged(QMediaRecorder::State)), this, SLOT(updateRecorderState(QMediaRecorder::State)));
+ connect(mediaRecorder, &QMediaRecorder::stateChanged, this, &Camera::updateRecorderState);
imageCapture = new QCameraImageCapture(camera);
- connect(mediaRecorder, SIGNAL(durationChanged(qint64)), this, SLOT(updateRecordTime()));
- connect(mediaRecorder, SIGNAL(error(QMediaRecorder::Error)), this, SLOT(displayRecorderError()));
+ connect(mediaRecorder, &QMediaRecorder::durationChanged, this, &Camera::updateRecordTime);
+ connect(mediaRecorder, QOverload<QMediaRecorder::Error>::of(&QMediaRecorder::error),
+ this, &Camera::displayRecorderError);
mediaRecorder->setMetaData(QMediaMetaData::Title, QVariant(QLatin1String("Test Title")));
- connect(ui->exposureCompensation, SIGNAL(valueChanged(int)), SLOT(setExposureCompensation(int)));
+ connect(ui->exposureCompensation, &QAbstractSlider::valueChanged, this, &Camera::setExposureCompensation);
camera->setViewfinder(ui->viewfinder);
@@ -123,14 +120,14 @@ void Camera::setCamera(const QCameraInfo &cameraInfo)
updateLockStatus(camera->lockStatus(), QCamera::UserRequest);
updateRecorderState(mediaRecorder->state());
- connect(imageCapture, SIGNAL(readyForCaptureChanged(bool)), this, SLOT(readyForCapture(bool)));
- connect(imageCapture, SIGNAL(imageCaptured(int,QImage)), this, SLOT(processCapturedImage(int,QImage)));
- connect(imageCapture, SIGNAL(imageSaved(int,QString)), this, SLOT(imageSaved(int,QString)));
- connect(imageCapture, SIGNAL(error(int,QCameraImageCapture::Error,QString)), this,
- SLOT(displayCaptureError(int,QCameraImageCapture::Error,QString)));
+ connect(imageCapture, &QCameraImageCapture::readyForCaptureChanged, this, &Camera::readyForCapture);
+ connect(imageCapture, &QCameraImageCapture::imageCaptured, this, &Camera::processCapturedImage);
+ connect(imageCapture, &QCameraImageCapture::imageSaved, this, &Camera::imageSaved);
+ connect(imageCapture, QOverload<int, QCameraImageCapture::Error, const QString &>::of(&QCameraImageCapture::error),
+ this, &Camera::displayCaptureError);
- connect(camera, SIGNAL(lockStatusChanged(QCamera::LockStatus,QCamera::LockChangeReason)),
- this, SLOT(updateLockStatus(QCamera::LockStatus,QCamera::LockChangeReason)));
+ connect(camera, QOverload<QCamera::LockStatus, QCamera::LockChangeReason>::of(&QCamera::lockStatusChanged),
+ this, &Camera::updateLockStatus);
ui->captureWidget->setTabEnabled(0, (camera->isCaptureModeSupported(QCamera::CaptureStillImage)));
ui->captureWidget->setTabEnabled(1, (camera->isCaptureModeSupported(QCamera::CaptureVideo)));
@@ -197,7 +194,7 @@ void Camera::processCapturedImage(int requestId, const QImage& img)
// Display captured image for 4 seconds.
displayCapturedImage();
- QTimer::singleShot(4000, this, SLOT(displayViewfinder()));
+ QTimer::singleShot(4000, this, &Camera::displayViewfinder);
}
void Camera::configureCaptureSettings()
@@ -217,6 +214,7 @@ void Camera::configureCaptureSettings()
void Camera::configureVideoSettings()
{
VideoSettings settingsDialog(mediaRecorder);
+ settingsDialog.setWindowFlags(settingsDialog.windowFlags() & ~Qt::WindowContextHelpButtonHint);
settingsDialog.setAudioSettings(audioSettings);
settingsDialog.setVideoSettings(videoSettings);
@@ -237,6 +235,7 @@ void Camera::configureVideoSettings()
void Camera::configureImageSettings()
{
ImageSettings settingsDialog(imageCapture);
+ settingsDialog.setWindowFlags(settingsDialog.windowFlags() & ~Qt::WindowContextHelpButtonHint);
settingsDialog.setImageSettings(imageSettings);
@@ -385,12 +384,12 @@ void Camera::setExposureCompensation(int index)
void Camera::displayRecorderError()
{
- QMessageBox::warning(this, tr("Capture error"), mediaRecorder->errorString());
+ QMessageBox::warning(this, tr("Capture Error"), mediaRecorder->errorString());
}
void Camera::displayCameraError()
{
- QMessageBox::warning(this, tr("Camera error"), camera->errorString());
+ QMessageBox::warning(this, tr("Camera Error"), camera->errorString());
}
void Camera::updateCameraDevice(QAction *action)
@@ -416,7 +415,7 @@ void Camera::readyForCapture(bool ready)
void Camera::imageSaved(int id, const QString &fileName)
{
Q_UNUSED(id);
- Q_UNUSED(fileName);
+ ui->statusbar->showMessage(tr("Captured \"%1\"").arg(QDir::toNativeSeparators(fileName)));
isCapturingImage = false;
if (applicationExiting)
diff --git a/examples/multimediawidgets/camera/camera.h b/examples/multimediawidgets/camera/camera.h
index 3db1347b6..45a73176c 100644
--- a/examples/multimediawidgets/camera/camera.h
+++ b/examples/multimediawidgets/camera/camera.h
@@ -56,7 +56,7 @@ class Camera : public QMainWindow
Q_OBJECT
public:
- Camera(QWidget *parent = 0);
+ explicit Camera(QWidget *parent = nullptr);
~Camera();
private slots:
@@ -107,16 +107,16 @@ protected:
private:
Ui::Camera *ui;
- QCamera *camera;
- QCameraImageCapture *imageCapture;
- QMediaRecorder* mediaRecorder;
+ QCamera *camera = nullptr;
+ QCameraImageCapture *imageCapture = nullptr;
+ QMediaRecorder* mediaRecorder = nullptr;
QImageEncoderSettings imageSettings;
QAudioEncoderSettings audioSettings;
QVideoEncoderSettings videoSettings;
QString videoContainerFormat;
- bool isCapturingImage;
- bool applicationExiting;
+ bool isCapturingImage = false;
+ bool applicationExiting = false;
};
#endif
diff --git a/examples/multimediawidgets/camera/imagesettings.cpp b/examples/multimediawidgets/camera/imagesettings.cpp
index 55441b4e3..658176369 100644
--- a/examples/multimediawidgets/camera/imagesettings.cpp
+++ b/examples/multimediawidgets/camera/imagesettings.cpp
@@ -56,16 +56,17 @@ ImageSettings::ImageSettings(QCameraImageCapture *imageCapture, QWidget *parent)
//image codecs
ui->imageCodecBox->addItem(tr("Default image format"), QVariant(QString()));
- foreach(const QString &codecName, imagecapture->supportedImageCodecs()) {
+ const QStringList supportedImageCodecs = imagecapture->supportedImageCodecs();
+ for (const QString &codecName : supportedImageCodecs) {
QString description = imagecapture->imageCodecDescription(codecName);
- ui->imageCodecBox->addItem(codecName+": "+description, QVariant(codecName));
+ ui->imageCodecBox->addItem(codecName + ": " + description, QVariant(codecName));
}
ui->imageQualitySlider->setRange(0, int(QMultimedia::VeryHighQuality));
ui->imageResolutionBox->addItem(tr("Default Resolution"));
- QList<QSize> supportedResolutions = imagecapture->supportedResolutions();
- foreach(const QSize &resolution, supportedResolutions) {
+ const QList<QSize> supportedResolutions = imagecapture->supportedResolutions();
+ for (const QSize &resolution : supportedResolutions) {
ui->imageResolutionBox->addItem(QString("%1x%2").arg(resolution.width()).arg(resolution.height()),
QVariant(resolution));
}
diff --git a/examples/multimediawidgets/camera/imagesettings.h b/examples/multimediawidgets/camera/imagesettings.h
index 31afbd2f5..5e23ef8e4 100644
--- a/examples/multimediawidgets/camera/imagesettings.h
+++ b/examples/multimediawidgets/camera/imagesettings.h
@@ -56,7 +56,7 @@ class ImageSettings : public QDialog
Q_OBJECT
public:
- ImageSettings(QCameraImageCapture *imageCapture, QWidget *parent = 0);
+ explicit ImageSettings(QCameraImageCapture *imageCapture, QWidget *parent = nullptr);
~ImageSettings();
QAudioEncoderSettings audioSettings() const;
diff --git a/examples/multimediawidgets/camera/imagesettings.ui b/examples/multimediawidgets/camera/imagesettings.ui
index f790c770d..8c59ca01d 100644
--- a/examples/multimediawidgets/camera/imagesettings.ui
+++ b/examples/multimediawidgets/camera/imagesettings.ui
@@ -11,7 +11,7 @@
</rect>
</property>
<property name="windowTitle">
- <string>Dialog</string>
+ <string>Image Settings</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
diff --git a/examples/multimediawidgets/camera/videosettings.cpp b/examples/multimediawidgets/camera/videosettings.cpp
index 3e0c2afd0..38e8e476b 100644
--- a/examples/multimediawidgets/camera/videosettings.cpp
+++ b/examples/multimediawidgets/camera/videosettings.cpp
@@ -56,47 +56,49 @@ VideoSettings::VideoSettings(QMediaRecorder *mediaRecorder, QWidget *parent) :
//audio codecs
ui->audioCodecBox->addItem(tr("Default audio codec"), QVariant(QString()));
- foreach (const QString &codecName, mediaRecorder->supportedAudioCodecs()) {
+ const QStringList supportedAudioCodecs = mediaRecorder->supportedAudioCodecs();
+ for (const QString &codecName : supportedAudioCodecs) {
QString description = mediaRecorder->audioCodecDescription(codecName);
- ui->audioCodecBox->addItem(codecName+": "+description, QVariant(codecName));
+ ui->audioCodecBox->addItem(codecName + ": " + description, QVariant(codecName));
}
//sample rate:
- foreach (int sampleRate, mediaRecorder->supportedAudioSampleRates()) {
+ const QList<int>supportedAudioSampleRates = mediaRecorder->supportedAudioSampleRates();
+ for (int sampleRate : supportedAudioSampleRates)
ui->audioSampleRateBox->addItem(QString::number(sampleRate), QVariant(sampleRate));
- }
ui->audioQualitySlider->setRange(0, int(QMultimedia::VeryHighQuality));
//video codecs
ui->videoCodecBox->addItem(tr("Default video codec"), QVariant(QString()));
- foreach (const QString &codecName, mediaRecorder->supportedVideoCodecs()) {
+ const QStringList supportedVideoCodecs = mediaRecorder->supportedVideoCodecs();
+ for (const QString &codecName : supportedVideoCodecs) {
QString description = mediaRecorder->videoCodecDescription(codecName);
- ui->videoCodecBox->addItem(codecName+": "+description, QVariant(codecName));
+ ui->videoCodecBox->addItem(codecName + ": " + description, QVariant(codecName));
}
ui->videoQualitySlider->setRange(0, int(QMultimedia::VeryHighQuality));
ui->videoResolutionBox->addItem(tr("Default"));
- QList<QSize> supportedResolutions = mediaRecorder->supportedResolutions();
- foreach (const QSize &resolution, supportedResolutions) {
+ const QList<QSize> supportedResolutions = mediaRecorder->supportedResolutions();
+ for (const QSize &resolution : supportedResolutions) {
ui->videoResolutionBox->addItem(QString("%1x%2").arg(resolution.width()).arg(resolution.height()),
QVariant(resolution));
}
ui->videoFramerateBox->addItem(tr("Default"));
- QList<qreal> supportedFrameRates = mediaRecorder->supportedFrameRates();
- qreal rate;
- foreach (rate, supportedFrameRates) {
+ const QList<qreal> supportedFrameRates = mediaRecorder->supportedFrameRates();
+ for (qreal rate : supportedFrameRates) {
QString rateString = QString("%1").arg(rate, 0, 'f', 2);
ui->videoFramerateBox->addItem(rateString, QVariant(rate));
}
//containers
ui->containerFormatBox->addItem(tr("Default container"), QVariant(QString()));
- foreach (const QString &format, mediaRecorder->supportedContainers()) {
- ui->containerFormatBox->addItem(format+":"+mediaRecorder->containerDescription(format),
+ const QStringList formats = mediaRecorder->supportedContainers();
+ for (const QString &format : formats) {
+ ui->containerFormatBox->addItem(format + ": " + mediaRecorder->containerDescription(format),
QVariant(format));
}
}
diff --git a/examples/multimediawidgets/camera/videosettings.h b/examples/multimediawidgets/camera/videosettings.h
index c4a05face..1751025fb 100644
--- a/examples/multimediawidgets/camera/videosettings.h
+++ b/examples/multimediawidgets/camera/videosettings.h
@@ -56,7 +56,7 @@ class VideoSettings : public QDialog
Q_OBJECT
public:
- VideoSettings(QMediaRecorder *mediaRecorder, QWidget *parent = 0);
+ explicit VideoSettings(QMediaRecorder *mediaRecorder, QWidget *parent = nullptr);
~VideoSettings();
QAudioEncoderSettings audioSettings() const;
diff --git a/examples/multimediawidgets/camera/videosettings.ui b/examples/multimediawidgets/camera/videosettings.ui
index f6aa004c5..940b7db7a 100644
--- a/examples/multimediawidgets/camera/videosettings.ui
+++ b/examples/multimediawidgets/camera/videosettings.ui
@@ -11,7 +11,7 @@
</rect>
</property>
<property name="windowTitle">
- <string>Dialog</string>
+ <string>Video Settings</string>
</property>
<layout class="QGridLayout" name="gridLayout_4">
<item row="0" column="0">