Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qscreen.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qscreen.h"
5#include "qscreen_p.h"
6#include "qpixmap.h"
7#include "qguiapplication_p.h"
8#include <qpa/qplatformscreen.h>
9#include <qpa/qplatformscreen_p.h>
10
11#include <QtCore/QDebug>
12#include <QtCore/private/qobject_p.h>
13#include "qhighdpiscaling_p.h"
14
16
40QScreen::QScreen(QPlatformScreen *platformScreen)
42{
43 Q_D(QScreen);
44
45 d->platformScreen = platformScreen;
46 platformScreen->d_func()->screen = this;
47
48 d->orientation = platformScreen->orientation();
49 d->logicalDpi = QPlatformScreen::overrideDpi(platformScreen->logicalDpi());
50 d->refreshRate = platformScreen->refreshRate();
51 // safeguard ourselves against buggy platform behavior...
52 if (d->refreshRate < 1.0)
53 d->refreshRate = 60.0;
54
55 d->updateGeometry();
56 d->updatePrimaryOrientation(); // derived from the geometry
57}
58
60{
62 QRect nativeGeometry = platformScreen->geometry();
63 geometry = QRect(nativeGeometry.topLeft(), QHighDpi::fromNative(nativeGeometry.size(), scaleFactor));
65}
66
73{
75 "QScreens should be removed via QWindowSystemInterface::handleScreenRemoved()");
76}
77
84{
85 Q_D(const QScreen);
86 return d->platformScreen;
87}
88
101{
102 Q_D(const QScreen);
103 return d->platformScreen->name();
104}
105
113{
114 Q_D(const QScreen);
115 return d->platformScreen->manufacturer();
116}
117
125{
126 Q_D(const QScreen);
127 return d->platformScreen->model();
128}
129
137{
138 Q_D(const QScreen);
139 return d->platformScreen->serialNumber();
140}
141
146int QScreen::depth() const
147{
148 Q_D(const QScreen);
149 return d->platformScreen->depth();
150}
151
157{
158 Q_D(const QScreen);
159 return d->geometry.size();
160}
161
176{
177 return size().width() / physicalSize().width() * qreal(25.4);
178}
179
194{
195 return size().height() / physicalSize().height() * qreal(25.4);
196}
197
216{
217 QSize sz = size();
218 QSizeF psz = physicalSize();
219 return ((sz.height() / psz.height()) + (sz.width() / psz.width())) * qreal(25.4 * 0.5);
220}
221
231{
232 Q_D(const QScreen);
234 return QHighDpiScaling::logicalDpi(this).first;
235 return d->logicalDpi.first;
236}
237
247{
248 Q_D(const QScreen);
250 return QHighDpiScaling::logicalDpi(this).second;
251 return d->logicalDpi.second;
252}
253
267{
268 Q_D(const QScreen);
269 QDpi dpi = QHighDpiScaling::isActive() ? QHighDpiScaling::logicalDpi(this) : d->logicalDpi;
270 return (dpi.first + dpi.second) * qreal(0.5);
271}
272
286{
287 Q_D(const QScreen);
288 return d->platformScreen->devicePixelRatio() * QHighDpiScaling::factor(this);
289}
290
302{
303 Q_D(const QScreen);
304 return d->platformScreen->physicalSize();
305}
306
315{
316 Q_D(const QScreen);
317 return d->availableGeometry.size();
318}
319
328{
329 Q_D(const QScreen);
330 return d->geometry;
331}
332
345{
346 Q_D(const QScreen);
347 return d->availableGeometry;
348}
349
357QList<QScreen *> QScreen::virtualSiblings() const
358{
359 Q_D(const QScreen);
360 const QList<QPlatformScreen *> platformScreens = d->platformScreen->virtualSiblings();
361 QList<QScreen *> screens;
362 screens.reserve(platformScreens.size());
363 for (QPlatformScreen *platformScreen : platformScreens) {
364 // Only consider platform screens that have been added
365 if (auto *knownScreen = platformScreen->screen())
366 screens << knownScreen;
367 }
368 return screens;
369}
370
382{
383 return virtualGeometry().size();
384}
385
397{
399 const auto screens = virtualSiblings();
400 for (QScreen *screen : screens)
401 result |= screen->geometry();
402 return result;
403}
404
419
431{
433 const auto screens = virtualSiblings();
434 for (QScreen *screen : screens)
436 return result;
437}
438
455{
456 Q_D(const QScreen);
457 return d->orientation;
458}
459
470{
471 Q_D(const QScreen);
472 return d->refreshRate;
473}
474
489{
490 Q_D(const QScreen);
491 return d->primaryOrientation;
492}
493
506{
507 Q_D(const QScreen);
508 return d->platformScreen->nativeOrientation();
509}
510
529
552
572
584
596
619
630{
631 const auto &siblings = virtualSiblings();
632 for (QScreen *sibling : siblings) {
633 if (sibling->geometry().contains(point))
634 return sibling;
635 }
636 return nullptr;
637}
638
686{
687 const QPlatformScreen *platformScreen = handle();
688 if (!platformScreen) {
689 qWarning("invoked with handle==0");
690 return QPixmap();
691 }
692 const qreal factor = QHighDpiScaling::factor(this);
693 if (qFuzzyCompare(factor, 1))
694 return platformScreen->grabWindow(window, x, y, width, height);
695
696 const QPoint nativePos = QHighDpi::toNative(QPoint(x, y), factor);
697 QSize nativeSize(width, height);
698 if (nativeSize.isValid())
699 nativeSize = QHighDpi::toNative(nativeSize, factor);
701 platformScreen->grabWindow(window, nativePos.x(), nativePos.y(),
702 nativeSize.width(), nativeSize.height());
703 result.setDevicePixelRatio(result.devicePixelRatio() * factor);
704 return result;
705}
706
720void *QScreen::resolveInterface(const char *name, int revision) const
721{
722 using namespace QNativeInterface;
723 using namespace QNativeInterface::Private;
724
725 auto *platformScreen = handle();
726 Q_UNUSED(platformScreen);
727 Q_UNUSED(name);
728 Q_UNUSED(revision);
729
730#if QT_CONFIG(xcb)
732#endif
733
734#if QT_CONFIG(vsp2)
735 QT_NATIVE_INTERFACE_RETURN_IF(QVsp2Screen, platformScreen);
736#endif
737
738#if defined(Q_OS_WEBOS)
739 QT_NATIVE_INTERFACE_RETURN_IF(QWebOSScreen, platformScreen);
740#endif
741
742#if defined(Q_OS_WIN32)
744#endif
745
746#if defined(Q_OS_ANDROID)
748#endif
749
750#if QT_CONFIG(wayland)
751 QT_NATIVE_INTERFACE_RETURN_IF(QWaylandScreen, platformScreen);
752#endif
753
754 return nullptr;
755}
756
757#ifndef QT_NO_DEBUG_STREAM
759{
760 const QDebugStateSaver saver(debug);
761 debug.nospace();
762 debug << "QScreen(" << (const void *)screen;
763 if (screen) {
764 debug << ", name=" << screen->name();
765 if (debug.verbosity() > 2) {
767 debug << ", primary";
768 debug << ", geometry=" << screen->geometry();
769 debug << ", available=" << screen->availableGeometry();
770 debug << ", logical DPI=" << screen->logicalDotsPerInchX()
771 << ',' << screen->logicalDotsPerInchY()
772 << ", physical DPI=" << screen->physicalDotsPerInchX()
773 << ',' << screen->physicalDotsPerInchY()
774 << ", devicePixelRatio=" << screen->devicePixelRatio()
775 << ", orientation=" << screen->orientation()
776 << ", physical size=" << screen->physicalSize().width()
777 << 'x' << screen->physicalSize().height() << "mm";
778 }
779 }
780 debug << ')';
781 return debug;
782}
783#endif // !QT_NO_DEBUG_STREAM
784
786{
787 initialState.platformScreen = screen->handle();
788
789 // Use public APIs to read out current state, rather
790 // than accessing the QScreenPrivate members, so that
791 // we detect any changes to the high-DPI scale factors
792 // that may be applied in the getters.
793
794 initialState.logicalDpi = QDpi{
797 };
798 initialState.geometry = screen->geometry();
801}
802
804{
805 QScreen *screen = initialState.platformScreen->screen();
806
807 const auto logicalDotsPerInch = QDpi{
810 };
811 if (logicalDotsPerInch != initialState.logicalDpi)
813
814 const auto geometry = screen->geometry();
815 const auto geometryChanged = geometry != initialState.geometry;
816 if (geometryChanged)
818
820 const auto availableGeometryChanged = availableGeometry != initialState.availableGeometry;
821 if (availableGeometryChanged)
823
824 if (geometryChanged || availableGeometryChanged) {
825 const auto siblings = screen->virtualSiblings();
826 for (QScreen* sibling : siblings)
827 emit sibling->virtualGeometryChanged(sibling->virtualGeometry());
828 }
829
830 if (geometryChanged) {
832
834 if (primaryOrientation != initialState.primaryOrientation)
836 }
837}
838
840
841#include "moc_qscreen.cpp"
\inmodule QtCore
\inmodule QtCore
static QList< QScreen * > screen_list
QScreen * primaryScreen
the primary (or default) screen of the application.
static bool isActive()
static qreal factor(C *context)
static QDpi logicalDpi(const QScreen *screen)
Native interface to a screen.
\inmodule QtCore
Definition qobject.h:103
Returns a copy of the pixmap that is transformed using the given transformation transform and transfo...
Definition qpixmap.h:27
The QPlatformScreen class provides an abstraction for visual displays.
static QDpi overrideDpi(const QDpi &in)
virtual Qt::ScreenOrientation orientation() const
Reimplement this function in subclass to return the current orientation of the screen,...
static QRect mapBetween(Qt::ScreenOrientation a, Qt::ScreenOrientation b, const QRect &rect)
virtual QRect geometry() const =0
Reimplement in subclass to return the pixel geometry of the screen.
QScreen * screen() const
virtual QDpi logicalDpi() const
Reimplement this function in subclass to return the logical horizontal and vertical dots per inch met...
virtual QPixmap grabWindow(WId window, int x, int y, int width, int height) const
This function is called when Qt needs to be able to grab the content of a window.
virtual QRect availableGeometry() const
Reimplement in subclass to return the pixel geometry of the available space This normally is the desk...
static QTransform transformBetween(Qt::ScreenOrientation a, Qt::ScreenOrientation b, const QRect &target)
static int angleBetween(Qt::ScreenOrientation a, Qt::ScreenOrientation b)
virtual qreal refreshRate() const
Reimplement this function in subclass to return the vertical refresh rate of the screen,...
\inmodule QtCore\reentrant
Definition qpoint.h:25
constexpr int x() const noexcept
Returns the x coordinate of this point.
Definition qpoint.h:130
constexpr int y() const noexcept
Returns the y coordinate of this point.
Definition qpoint.h:135
\inmodule QtCore\reentrant
Definition qrect.h:30
constexpr int height() const noexcept
Returns the height of the rectangle.
Definition qrect.h:239
constexpr QPoint topLeft() const noexcept
Returns the position of the rectangle's top-left corner.
Definition qrect.h:221
constexpr QSize size() const noexcept
Returns the size of the rectangle.
Definition qrect.h:242
constexpr int width() const noexcept
Returns the width of the rectangle.
Definition qrect.h:236
UpdateEmitter(QScreen *screen)
Definition qscreen.cpp:785
void updatePrimaryOrientation()
Definition qscreen.cpp:615
void updateGeometry()
Definition qscreen.cpp:59
The QScreen class is used to query screen properties. \inmodule QtGui.
Definition qscreen.h:32
QSize availableVirtualSize
the available size of the virtual desktop to which this screen belongs
Definition qscreen.h:44
qreal logicalDotsPerInchY
the number of logical dots or pixels per inch in the vertical direction
Definition qscreen.h:57
qreal devicePixelRatio
the screen's ratio between physical pixels and device-independent pixels
Definition qscreen.h:59
QString model
the model of the screen
Definition qscreen.h:38
bool isPortrait(Qt::ScreenOrientation orientation) const
Convenience function that returns true if o is either portrait or inverted portrait; otherwise return...
Definition qscreen.cpp:579
bool isLandscape(Qt::ScreenOrientation orientation) const
Convenience function that returns true if o is either landscape or inverted landscape; otherwise retu...
Definition qscreen.cpp:591
int angleBetween(Qt::ScreenOrientation a, Qt::ScreenOrientation b) const
Convenience function to compute the angle of rotation to get from rotation a to rotation b.
Definition qscreen.cpp:519
Qt::ScreenOrientation nativeOrientation
the native screen orientation
Definition qscreen.h:63
QTransform transformBetween(Qt::ScreenOrientation a, Qt::ScreenOrientation b, const QRect &target) const
Convenience function to compute a transform that maps from the coordinate system defined by orientati...
Definition qscreen.cpp:542
qreal logicalDotsPerInchX
the number of logical dots or pixels per inch in the horizontal direction
Definition qscreen.h:56
QList< QScreen * > virtualSiblings() const
Get the screen's virtual siblings.
Definition qscreen.cpp:357
QRect availableGeometry
the screen's available geometry in pixels
Definition qscreen.h:46
QSizeF physicalSize
the screen's physical size (in millimeters)
Definition qscreen.h:50
QRect geometry
the screen's geometry in pixels
Definition qscreen.h:45
QPixmap grabWindow(WId window=0, int x=0, int y=0, int w=-1, int h=-1)
Creates and returns a pixmap constructed by grabbing the contents of the given window restricted by Q...
Definition qscreen.cpp:685
qreal refreshRate
the approximate vertical refresh rate of the screen in Hz
Definition qscreen.h:64
qreal physicalDotsPerInchY
the number of physical dots or pixels per inch in the vertical direction
Definition qscreen.h:54
QSize size
the pixel resolution of the screen
Definition qscreen.h:41
Qt::ScreenOrientation primaryOrientation
the primary screen orientation
Definition qscreen.h:61
QRect virtualGeometry
the pixel geometry of the virtual desktop to which this screen belongs
Definition qscreen.h:47
QSize virtualSize
the pixel size of the virtual desktop to which this screen belongs
Definition qscreen.h:43
void logicalDotsPerInchChanged(qreal dpi)
void availableGeometryChanged(const QRect &geometry)
int depth
the color depth of the screen
Definition qscreen.h:40
void geometryChanged(const QRect &geometry)
QString manufacturer
the manufacturer of the screen
Definition qscreen.h:37
Qt::ScreenOrientation orientation
the screen orientation
Definition qscreen.h:62
QString name
a user presentable string representing the screen
Definition qscreen.h:36
QSize availableSize
the screen's available size in pixels
Definition qscreen.h:42
qreal logicalDotsPerInch
the number of logical dots or pixels per inch
Definition qscreen.h:58
qreal physicalDotsPerInchX
the number of physical dots or pixels per inch in the horizontal direction
Definition qscreen.h:52
QString serialNumber
the serial number of the screen
Definition qscreen.h:39
void primaryOrientationChanged(Qt::ScreenOrientation orientation)
This signal is emitted when the primary orientation of the screen changes with orientation as an argu...
QScreen * virtualSiblingAt(QPoint point)
Returns the screen at point within the set of \l QScreen::virtualSiblings(), or nullptr if outside of...
Definition qscreen.cpp:629
~QScreen()
Destroys the screen.
Definition qscreen.cpp:72
void physicalDotsPerInchChanged(qreal dpi)
QRect mapBetween(Qt::ScreenOrientation a, Qt::ScreenOrientation b, const QRect &rect) const
Maps the rect between two screen orientations.
Definition qscreen.cpp:562
QPlatformScreen * handle() const
Get the platform screen handle.
Definition qscreen.cpp:83
QRect availableVirtualGeometry
the available geometry of the virtual desktop to which this screen belongs
Definition qscreen.h:49
qreal physicalDotsPerInch
the number of physical dots or pixels per inch
Definition qscreen.h:55
\inmodule QtCore
Definition qsize.h:208
constexpr qreal width() const noexcept
Returns the width.
Definition qsize.h:332
constexpr qreal height() const noexcept
Returns the height.
Definition qsize.h:335
\inmodule QtCore
Definition qsize.h:25
constexpr int height() const noexcept
Returns the height.
Definition qsize.h:133
constexpr int width() const noexcept
Returns the width.
Definition qsize.h:130
constexpr bool isValid() const noexcept
Returns true if both the width and height is equal to or greater than 0; otherwise returns false.
Definition qsize.h:127
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
The QTransform class specifies 2D transformations of a coordinate system.
Definition qtransform.h:20
Windows screen.
rect
[4]
T toNative(const T &value, qreal scaleFactor, QPoint origin=QPoint(0, 0))
T fromNative(const T &value, qreal scaleFactor, QPoint origin=QPoint(0, 0))
Combined button and popup list for selecting options.
ScreenOrientation
Definition qnamespace.h:271
@ InvertedLandscapeOrientation
Definition qnamespace.h:276
@ InvertedPortraitOrientation
Definition qnamespace.h:275
@ LandscapeOrientation
Definition qnamespace.h:274
@ PortraitOrientation
Definition qnamespace.h:273
@ PrimaryOrientation
Definition qnamespace.h:272
bool qFuzzyCompare(qfloat16 p1, qfloat16 p2) noexcept
Definition qfloat16.h:333
QPair< qreal, qreal > QDpi
#define qWarning
Definition qlogging.h:166
#define QT_NATIVE_INTERFACE_RETURN_IF(NativeInterface, baseType)
static bool contains(const QJsonArray &haystack, unsigned needle)
Definition qopengl.cpp:116
GLboolean GLboolean GLboolean b
GLint GLint GLint GLint GLint x
[0]
GLint GLsizei GLsizei height
GLboolean GLboolean GLboolean GLboolean a
[7]
GLint GLsizei width
GLenum target
GLuint name
GLint y
GLuint64EXT * result
[6]
#define Q_ASSERT_X(cond, x, msg)
Definition qrandom.cpp:48
Q_GUI_EXPORT QDebug operator<<(QDebug debug, const QScreen *screen)
Definition qscreen.cpp:758
QScreen * screen
[1]
Definition main.cpp:29
#define emit
#define Q_UNUSED(x)
double qreal
Definition qtypes.h:187
QObject::connect nullptr
aWidget window() -> setWindowTitle("New Window Title")
[2]
QDpi logicalDpi
Definition qscreen_p.h:35
QPlatformScreen * platformScreen
Definition qscreen_p.h:29
QRect availableGeometry
Definition qscreen_p.h:34
QRect geometry
Definition qscreen_p.h:33
Qt::ScreenOrientation primaryOrientation
Definition qscreen_p.h:32