summaryrefslogtreecommitdiffstats
path: root/examples/multimedia/screencapture/screenlistmodel.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'examples/multimedia/screencapture/screenlistmodel.cpp')
-rw-r--r--examples/multimedia/screencapture/screenlistmodel.cpp54
1 files changed, 54 insertions, 0 deletions
diff --git a/examples/multimedia/screencapture/screenlistmodel.cpp b/examples/multimedia/screencapture/screenlistmodel.cpp
new file mode 100644
index 000000000..0b46560cc
--- /dev/null
+++ b/examples/multimedia/screencapture/screenlistmodel.cpp
@@ -0,0 +1,54 @@
+// Copyright (C) 2022 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
+
+#include "screenlistmodel.h"
+
+#include <QGuiApplication>
+#include <QScreen>
+
+#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 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());
+ QString description;
+ QTextStream str(&description);
+ str << '"' << screen->name() << "\" " << screen->size().width() << 'x'
+ << screen->size().height() << ", " << screen->logicalDotsPerInch() << "DPI";
+ return description;
+ }
+
+ return {};
+}
+
+QScreen *ScreenListModel::screen(const QModelIndex &index) const
+{
+ return QGuiApplication::screens().value(index.row());
+}
+
+void ScreenListModel::screensChanged()
+{
+ beginResetModel();
+ endResetModel();
+}
+
+#include "moc_screenlistmodel.cpp"