summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFriedemann Kleint <Friedemann.Kleint@qt.io>2023-04-05 10:10:50 +0200
committerFriedemann Kleint <Friedemann.Kleint@qt.io>2023-04-13 22:21:44 +0200
commitc259c052594c33ac304f7103333b596fd024e673 (patch)
treec72d33bdb326c14652e94ba043ebc097d5fa2104
parent5e36c05dac20a997c5be7a8445daf78758fb275b (diff)
downloadqtmultimedia-c259c052594c33ac304f7103333b596fd024e673.tar.gz
Brush up the screencapture example
- Fix compiler warnings about unused parameters - Fix spacing - Reorder includes by module - Translate user-visible texts - Use QList::size() instead of deprecated count() - Fix clang warnings about else after return - Streamline some code Pick-to: 6.5 Change-Id: Ic7640d72018b59f05c84224ca006a3f60a26f7ea Reviewed-by: Artem Dyomin <artem.dyomin@qt.io> Reviewed-by: Lars Knoll <lars@knoll.priv.no>
-rw-r--r--examples/multimedia/screencapture/screencapturepreview.cpp34
-rw-r--r--examples/multimedia/screencapture/screencapturepreview.h11
-rw-r--r--examples/multimedia/screencapture/screenlistmodel.cpp15
-rw-r--r--examples/multimedia/screencapture/screenlistmodel.h2
-rw-r--r--examples/multimedia/screencapture/windowlistmodel.cpp10
5 files changed, 34 insertions, 38 deletions
diff --git a/examples/multimedia/screencapture/screencapturepreview.cpp b/examples/multimedia/screencapture/screencapturepreview.cpp
index 1d514596b..f7aee01fe 100644
--- a/examples/multimedia/screencapture/screencapturepreview.cpp
+++ b/examples/multimedia/screencapture/screencapturepreview.cpp
@@ -5,22 +5,26 @@
#include "screenlistmodel.h"
#include "windowlistmodel.h"
-#include <QGuiApplication>
-#include <QGridLayout>
-#include <QListWidget>
#include <QMediaCaptureSession>
-#include <QPushButton>
#include <QScreenCapture>
-#include <QLineEdit>
#include <QVideoWidget>
-#include <QMessageBox>
+
+#include <QGridLayout>
#include <QLabel>
+#include <QLineEdit>
+#include <QListWidget>
+#include <QMessageBox>
+#include <QPushButton>
+
+#include <QGuiApplication>
ScreenCapturePreview::ScreenCapturePreview(QWidget *parent)
: QWidget(parent),
screenListView(new QListView(this)),
windowListView(new QListView(this)),
screenCapture(new QScreenCapture(this)),
+ screens(QGuiApplication::screens()),
+ windows(QGuiApplication::allWindows()),
mediaCaptureSession(new QMediaCaptureSession(this)),
videoWidget(new QVideoWidget(this)),
gridLayout(new QGridLayout(this)),
@@ -34,9 +38,6 @@ ScreenCapturePreview::ScreenCapturePreview(QWidget *parent)
videoWidgetLabel(new QLabel("QScreenCapture output:", this))
{
// Get lists of screens and windows:
-
- screens = QGuiApplication::screens();
- windows = QGuiApplication::allWindows();
screenListModel = new ScreenListModel(screens, this);
windowListModel = new WindowListModel(windows, this);
qDebug() << "return value from QGuiApplication::screens(): " << screens;
@@ -44,7 +45,7 @@ ScreenCapturePreview::ScreenCapturePreview(QWidget *parent)
// Setup QScreenCapture with initial source:
- screenCapture->setScreen((!screens.isEmpty()) ? screens.first() : nullptr);
+ screenCapture->setScreen(QGuiApplication::primaryScreen());
screenCapture->start();
mediaCaptureSession->setScreenCapture(screenCapture);
mediaCaptureSession->setVideoOutput(videoWidget);
@@ -54,7 +55,7 @@ ScreenCapturePreview::ScreenCapturePreview(QWidget *parent)
screenListView->setModel(screenListModel);
windowListView->setModel(windowListModel);
// Sets initial lineEdit text to the WId of the second QWindow from QGuiApplication::allWindows()
- if (windows.count() > 1)
+ if (windows.size() > 1)
lineEdit->setText(QString::number(windows[1]->winId()));
gridLayout->addWidget(screenLabel, 0, 0);
@@ -81,12 +82,9 @@ ScreenCapturePreview::ScreenCapturePreview(QWidget *parent)
connect(startStopButton, &QPushButton::clicked, this, &ScreenCapturePreview::onStartStopButtonClicked);
connect(lineEdit, &QLineEdit::returnPressed, this, &ScreenCapturePreview::onWindowIdSelectionChanged);
connect(screenCapture, &QScreenCapture::errorOccurred, this, &ScreenCapturePreview::onScreenCaptureErrorOccured);
-
}
-ScreenCapturePreview::~ScreenCapturePreview()
-{
-}
+ScreenCapturePreview::~ScreenCapturePreview() = default;
void ScreenCapturePreview::onScreenSelectionChanged(QModelIndex index)
{
@@ -100,7 +98,7 @@ void ScreenCapturePreview::onWindowSelectionChanged(QModelIndex index)
void ScreenCapturePreview::onWindowIdSelectionChanged()
{
- unsigned long long input = lineEdit->text().toULongLong(nullptr, 0);
+ unsigned long long input = lineEdit->text().trimmed().toULongLong(nullptr, 0);
screenCapture->setWindowId(input);
}
@@ -114,10 +112,10 @@ void ScreenCapturePreview::onStartStopButtonClicked()
{
if (screenCapture->isActive()) {
screenCapture->stop();
- startStopButton->setText("Start screencapture");
+ startStopButton->setText(tr("Start screencapture"));
} else {
screenCapture->start();
- startStopButton->setText("Stop screencapture");
+ startStopButton->setText(tr("Stop screencapture"));
}
}
diff --git a/examples/multimedia/screencapture/screencapturepreview.h b/examples/multimedia/screencapture/screencapturepreview.h
index e3ae60711..fd5cc08ee 100644
--- a/examples/multimedia/screencapture/screencapturepreview.h
+++ b/examples/multimedia/screencapture/screencapturepreview.h
@@ -29,12 +29,10 @@ class ScreenCapturePreview : public QWidget
Q_OBJECT
public:
-
- ScreenCapturePreview(QWidget *parent = nullptr);
+ explicit ScreenCapturePreview(QWidget *parent = nullptr);
~ScreenCapturePreview();
public slots:
-
void onScreenSelectionChanged(QModelIndex index);
void onWindowSelectionChanged(QModelIndex index);
void onWindowIdSelectionChanged();
@@ -42,14 +40,13 @@ public slots:
void onStartStopButtonClicked();
private:
-
ScreenListModel *screenListModel = nullptr;
WindowListModel *windowListModel = nullptr;
QListView *screenListView = nullptr;
QListView *windowListView = nullptr;
QScreenCapture *screenCapture = nullptr;
- QList<QScreen *> screens = QList<QScreen *>();
- QList<QWindow *> windows = QList<QWindow *>();
+ QList<QScreen *> screens;
+ QWindowList windows;
QMediaCaptureSession *mediaCaptureSession = nullptr;
QVideoWidget *videoWidget = nullptr;
QGridLayout *gridLayout = nullptr;
@@ -61,6 +58,6 @@ private:
QLabel *windowLabel = nullptr;
QLabel *windowIdLabel = nullptr;
QLabel *videoWidgetLabel = nullptr;
-
};
+
#endif // SCREENCAPTUREPREVIEW_H
diff --git a/examples/multimedia/screencapture/screenlistmodel.cpp b/examples/multimedia/screencapture/screenlistmodel.cpp
index 0b0662a17..6a3a1efd7 100644
--- a/examples/multimedia/screencapture/screenlistmodel.cpp
+++ b/examples/multimedia/screencapture/screenlistmodel.cpp
@@ -5,28 +5,29 @@
#include <QScreen>
-ScreenListModel::ScreenListModel(QList<QScreen *> data, QObject *parent)
- : QAbstractListModel(parent), screenList(data)
+ScreenListModel::ScreenListModel(const QList<QScreen *> &data, QObject *parent) :
+ QAbstractListModel(parent),
+ screenList(data)
{
}
-int ScreenListModel::rowCount(const QModelIndex &parent) const
+int ScreenListModel::rowCount(const QModelIndex &) const
{
- return screenList.count();
+ return screenList.size();
}
QVariant ScreenListModel::data(const QModelIndex &index, int role) const
{
Q_ASSERT(index.isValid());
- Q_ASSERT(index.row() <= screenList.count());
+ Q_ASSERT(index.row() <= screenList.size());
if (role == Qt::DisplayRole) {
auto screen = screenList.at(index.row());
return QString("%1: %2").arg(screen->metaObject()->className(),
screen->name());
- } else {
- return QVariant();
}
+
+ return {};
}
QScreen *ScreenListModel::screen(const QModelIndex &index) const
diff --git a/examples/multimedia/screencapture/screenlistmodel.h b/examples/multimedia/screencapture/screenlistmodel.h
index 4069a1f3f..83cc1e664 100644
--- a/examples/multimedia/screencapture/screenlistmodel.h
+++ b/examples/multimedia/screencapture/screenlistmodel.h
@@ -17,7 +17,7 @@ class ScreenListModel : public QAbstractListModel
Q_OBJECT
public:
- explicit ScreenListModel(QList<QScreen *> data = QList<QScreen *>(), QObject *parent = nullptr);
+ explicit ScreenListModel(const QList<QScreen *> &data, QObject *parent = nullptr);
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
diff --git a/examples/multimedia/screencapture/windowlistmodel.cpp b/examples/multimedia/screencapture/windowlistmodel.cpp
index 8772223c9..aba28a1db 100644
--- a/examples/multimedia/screencapture/windowlistmodel.cpp
+++ b/examples/multimedia/screencapture/windowlistmodel.cpp
@@ -10,15 +10,15 @@ WindowListModel::WindowListModel(QList<QWindow *> data, QObject *parent)
{
}
-int WindowListModel::rowCount(const QModelIndex &parent) const
+int WindowListModel::rowCount(const QModelIndex &) const
{
- return windowList.count();
+ return windowList.size();
}
QVariant WindowListModel::data(const QModelIndex &index, int role) const
{
Q_ASSERT(index.isValid());
- Q_ASSERT(index.row() <= windowList.count());
+ Q_ASSERT(index.row() <= windowList.size());
if (role == Qt::DisplayRole) {
auto window = windowList.at(index.row());
@@ -26,9 +26,9 @@ QVariant WindowListModel::data(const QModelIndex &index, int role) const
.arg(window->metaObject()->className())
.arg(window->winId())
.arg(window->objectName());
- } else {
- return QVariant();
}
+
+ return {};
}
QWindow *WindowListModel::window(const QModelIndex &index) const