summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFriedemann Kleint <Friedemann.Kleint@qt.io>2023-04-05 10:58:31 +0200
committerFriedemann Kleint <Friedemann.Kleint@qt.io>2023-04-20 18:37:47 +0200
commit654f5932445faca27d254db1e5c377de78ac4e7c (patch)
treebe3d751965cc56e1baaf6f1ca7ddfdd948a13213
parenta27a2a648c7b801fa89b561ddb65a3840d5e08dc (diff)
downloadqtmultimedia-654f5932445faca27d254db1e5c377de78ac4e7c.tar.gz
screencapture example: Make the screen model dynamic
Use QGuiApplication::screens() to obtain the screens and connect the changed signals to model reset. Make the description a bit more verbose. Change-Id: I92c0be712196aa7be475c08305ef6f8a20e603d3 Reviewed-by: Lars Knoll <lars@knoll.priv.no> Reviewed-by: Artem Dyomin <artem.dyomin@qt.io> (cherry picked from commit 004c18bb572329778a3e85fe436e0b216cf1f287)
-rw-r--r--examples/multimedia/screencapture/screencapturepreview.cpp4
-rw-r--r--examples/multimedia/screencapture/screencapturepreview.h2
-rw-r--r--examples/multimedia/screencapture/screenlistmodel.cpp32
-rw-r--r--examples/multimedia/screencapture/screenlistmodel.h6
4 files changed, 29 insertions, 15 deletions
diff --git a/examples/multimedia/screencapture/screencapturepreview.cpp b/examples/multimedia/screencapture/screencapturepreview.cpp
index 728e89220..9f40c8c80 100644
--- a/examples/multimedia/screencapture/screencapturepreview.cpp
+++ b/examples/multimedia/screencapture/screencapturepreview.cpp
@@ -21,7 +21,6 @@ ScreenCapturePreview::ScreenCapturePreview(QWidget *parent)
: QWidget(parent),
screenListView(new QListView(this)),
screenCapture(new QScreenCapture(this)),
- screens(QGuiApplication::screens()),
mediaCaptureSession(new QMediaCaptureSession(this)),
videoWidget(new QVideoWidget(this)),
gridLayout(new QGridLayout(this)),
@@ -30,8 +29,7 @@ ScreenCapturePreview::ScreenCapturePreview(QWidget *parent)
videoWidgetLabel(new QLabel("QScreenCapture output:", this))
{
// Get list of screens:
- screenListModel = new ScreenListModel(screens, this);
- qDebug() << "return value from QGuiApplication::screens(): " << screens;
+ screenListModel = new ScreenListModel(this);
// Setup QScreenCapture with initial source:
diff --git a/examples/multimedia/screencapture/screencapturepreview.h b/examples/multimedia/screencapture/screencapturepreview.h
index 72b639639..da665ab08 100644
--- a/examples/multimedia/screencapture/screencapturepreview.h
+++ b/examples/multimedia/screencapture/screencapturepreview.h
@@ -38,7 +38,7 @@ private:
ScreenListModel *screenListModel = nullptr;
QListView *screenListView = nullptr;
QScreenCapture *screenCapture = nullptr;
- QList<QScreen *> screens;
+ QWindowList windows;
QMediaCaptureSession *mediaCaptureSession = nullptr;
QVideoWidget *videoWidget = nullptr;
QGridLayout *gridLayout = nullptr;
diff --git a/examples/multimedia/screencapture/screenlistmodel.cpp b/examples/multimedia/screencapture/screenlistmodel.cpp
index 6a3a1efd7..0b46560cc 100644
--- a/examples/multimedia/screencapture/screenlistmodel.cpp
+++ b/examples/multimedia/screencapture/screenlistmodel.cpp
@@ -3,28 +3,38 @@
#include "screenlistmodel.h"
+#include <QGuiApplication>
#include <QScreen>
-ScreenListModel::ScreenListModel(const QList<QScreen *> &data, QObject *parent) :
- QAbstractListModel(parent),
- screenList(data)
+#include <QTextStream>
+
+ScreenListModel::ScreenListModel(QObject *parent) :
+ QAbstractListModel(parent)
{
+ auto *app = qApp;
+ connect(app, &QGuiApplication::screenAdded, this, &ScreenListModel::screensChanged);
+ connect(app, &QGuiApplication::screenRemoved, this, &ScreenListModel::screensChanged);
+ connect(app, &QGuiApplication::primaryScreenChanged, this, &ScreenListModel::screensChanged);
}
int ScreenListModel::rowCount(const QModelIndex &) const
{
- return screenList.size();
+ return QGuiApplication::screens().size();
}
QVariant ScreenListModel::data(const QModelIndex &index, int role) const
{
+ const auto screenList = QGuiApplication::screens();
Q_ASSERT(index.isValid());
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());
+ auto *screen = screenList.at(index.row());
+ QString description;
+ QTextStream str(&description);
+ str << '"' << screen->name() << "\" " << screen->size().width() << 'x'
+ << screen->size().height() << ", " << screen->logicalDotsPerInch() << "DPI";
+ return description;
}
return {};
@@ -32,7 +42,13 @@ QVariant ScreenListModel::data(const QModelIndex &index, int role) const
QScreen *ScreenListModel::screen(const QModelIndex &index) const
{
- return screenList.at(index.row());
+ return QGuiApplication::screens().value(index.row());
+}
+
+void ScreenListModel::screensChanged()
+{
+ beginResetModel();
+ endResetModel();
}
#include "moc_screenlistmodel.cpp"
diff --git a/examples/multimedia/screencapture/screenlistmodel.h b/examples/multimedia/screencapture/screenlistmodel.h
index 83cc1e664..3bfa30b8f 100644
--- a/examples/multimedia/screencapture/screenlistmodel.h
+++ b/examples/multimedia/screencapture/screenlistmodel.h
@@ -17,14 +17,14 @@ class ScreenListModel : public QAbstractListModel
Q_OBJECT
public:
- explicit ScreenListModel(const QList<QScreen *> &data, QObject *parent = nullptr);
+ explicit ScreenListModel(QObject *parent = nullptr);
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
QScreen *screen(const QModelIndex &index) const;
-private:
- QList<QScreen *> screenList;
+private Q_SLOTS:
+ void screensChanged();
};
#endif // SCREENLISTMODEL_H