1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 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"
|