Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qquickapplication.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// Qt-Security score:significant reason:default
4
5#include <QtQuick/private/qquickapplication_p.h>
6
7#include <QtGui/private/qguiapplication_p.h>
8#include <QtGui/qpa/qplatformintegration.h>
9#include <QtGui/qguiapplication.h>
10
11#include <QtQml/private/qqmlglobal_p.h>
12
13#include <QtCore/private/qobject_p.h>
14#include <QtCore/qdebug.h>
15
16QT_BEGIN_NAMESPACE
17
18/*!
19 \qmltype Application
20 \nativetype QQuickApplication
21 \inqmlmodule QtQuick
22 //! once exposed: \inherits CoreApplication?
23 //! TODO: \ingroup ?
24
25 \brief Provides access to global application
26 state properties shared by many QML components.
27
28 The Application singleton exposes a subset of QApplication's properties to
29 QML applications.
30
31 It also provides an aboutToQuit() signal, which is the same as
32 QCoreApplication::aboutToQuit().
33
34 \qml
35 import QtQuick
36
37 Window {
38 id: root
39 visible: true
40 width: 800
41 height: 680
42
43 title: `${Application.name} (${Application.version})`
44
45 Connections {
46 target: Application
47 function onAboutToQuit() {
48 console.log("Bye!")
49 }
50 }
51 }
52 \endqml
53
54 \sa SystemPalette
55*/
56
57/*!
58 \qmlproperty bool Application::active
59 \deprecated [5.2]
60
61 Returns whether the application is active.
62 Use Application.state == Qt.ApplicationActive instead
63*/
64
65/*!
66 \qmlproperty enumeration Application::state
67
68 This property represents the current state of the application.
69
70 See the C++ \l Qt::ApplicationState enum for possible values.
71
72 \qml
73 Timer {
74 interval: 1000; repeat: true
75 active: Application.state === Qt.ApplicationActive
76 onTriggered: imageFetcher.fetchLatestImages()
77 }
78 \endqml
79*/
80
81/*!
82 \qmlproperty enumeration Application::layoutDirection
83
84 This read-only property can be used to query the default layout
85 direction of the application. On system start-up, the default layout
86 direction depends on the application's language. The property has a
87 value of \c Qt.RightToLeft in locales where text and graphic elements
88 are read from right to left, and \c Qt.LeftToRight where the reading
89 direction flows from left to right. You can bind to this property to
90 customize your application layouts to support both layout directions.
91
92 See the C++ \l Qt::LayoutDirection enum for possible values.
93
94 \qml
95 RowLayout {
96 layoutDirection: Application.layoutDirection
97 }
98 \endqml
99*/
100
101/*!
102 \qmlproperty bool Application::supportsMultipleWindows
103
104 Returns \c true if the platform supports multiple windows. Some embedded
105 platforms do not support multiple windows, for example.
106 */
107
108/*!
109 \qmlproperty font Application::font
110 Returns the default application font as returned by
111 \l QGuiApplication::font().
112*/
113
114/*!
115 \qmlproperty string Application::displayName
116
117 This property represents the application display name set on the
118 QGuiApplication instance. This property can be written to in order to set
119 the application display name.
120
121 \qml
122 Binding {
123 target: Application
124 property: "displayName"
125 value: "My Awesome Application"
126 }
127 \endqml
128*/
129
130/*!
131 \qmlproperty list<Screen> Application::screens
132
133 An array containing the descriptions of all connected screens. The
134 elements of the array are objects with the same properties as the
135 \l{Screen} attached object. In practice the array corresponds to the screen
136 list returned by QGuiApplication::screens(). In addition to examining
137 properties like name, width, height, etc., the array elements can also be
138 assigned to the screen property of Window items, thus serving as an
139 alternative to the C++ side's QWindow::setScreen().
140
141 \sa Screen, Window, {Window::screen}{Window.screen}
142*/
143
144/* The following properties are from QQmlApplication.
145 ### Document those in QQmlApplication instead once it is exposed
146*/
147
148/*!
149 \qmlproperty list<string> Application::arguments
150
151 This is a string list of the arguments the executable was invoked with.
152 */
153
154/*!
155 \qmlproperty string Application::name
156
157 This is the application name set on the QCoreApplication instance. This
158 property can be written to in order to set the application name.
159 */
160
161/*!
162 \qmlproperty string Application::version
163
164 This is the application version set on the QCoreApplication instance. This
165 property can be written to in order to set the application version.
166 */
167
168/*!
169 \qmlproperty string Application::organization
170
171 This is the organization name set on the QCoreApplication instance.
172 This property can be written to in order to set the organization name.
173 */
174
175/*!
176 \qmlproperty string Application::domain
177
178 This is the organization domain set on the QCoreApplication instance.
179 This property can be written to in order to set the organization domain.
180 */
181
182/*!
183 \qmlproperty StyleHints Application::styleHints
184
185 The \c styleHints property provides platform-specific style hints and settings.
186 See the \l QStyleHints documentation for further details.
187
188 The following example uses \c styleHints to determine whether an
189 item should gain focus on mouse press or touch release:
190 \code
191 import QtQuick
192
193 MouseArea {
194 id: button
195
196 onPressed: {
197 if (!Application.styleHints.setFocusOnTouchRelease)
198 button.forceActiveFocus()
199 }
200 onReleased: {
201 if (Application.styleHints.setFocusOnTouchRelease)
202 button.forceActiveFocus()
203 }
204 }
205 \endcode
206 */
207
208/*!
209 \qmlsignal Application::aboutToQuit()
210
211 This signal is emitted when the application is about to quit the main
212 event loop. The signal is particularly useful if your application has to
213 do some last-second cleanup. User interaction is not possible in this state.
214 For more information, see \l {Window::closing()}{Window.closing}.
215
216 \sa QCoreApplication::aboutToQuit
217*/
218QQuickApplication::QQuickApplication(QObject *parent)
219 : QQmlApplication(parent)
220{
221 QCoreApplication *app = QCoreApplication::instance();
222 if (QGuiApplication *guiApp = qobject_cast<QGuiApplication *>(app)) {
223 connect(guiApp, &QGuiApplication::layoutDirectionChanged,
224 this, &QQuickApplication::layoutDirectionChanged);
225 connect(guiApp, &QGuiApplication::applicationStateChanged,
226 this, &QQuickApplication::stateChanged);
227 connect(guiApp, &QGuiApplication::applicationStateChanged,
228 this, &QQuickApplication::activeChanged);
229 connect(guiApp, &QGuiApplication::applicationDisplayNameChanged,
230 this, &QQuickApplication::displayNameChanged);
231
232 connect(guiApp, &QGuiApplication::primaryScreenChanged, this, &QQuickApplication::updateScreens);
233 connect(guiApp, &QGuiApplication::screenAdded, this, &QQuickApplication::updateScreens);
234 connect(guiApp, &QGuiApplication::screenRemoved, this, &QQuickApplication::updateScreens);
235 updateScreens();
236 }
237}
238
239QQuickApplication::~QQuickApplication()
240{
241}
242
243bool QQuickApplication::active() const
244{
245 return QGuiApplication::applicationState() == Qt::ApplicationActive;
246}
247
248Qt::LayoutDirection QQuickApplication::layoutDirection() const
249{
250 return QGuiApplication::layoutDirection();
251}
252
253bool QQuickApplication::supportsMultipleWindows() const
254{
255 return QGuiApplicationPrivate::platformIntegration()->hasCapability(QPlatformIntegration::MultipleWindows);
256}
257
258Qt::ApplicationState QQuickApplication::state() const
259{
260 return QGuiApplication::applicationState();
261}
262
263QFont QQuickApplication::font() const
264{
265 return QGuiApplication::font();
266}
267
268QString QQuickApplication::displayName() const
269{
270 return QGuiApplication::applicationDisplayName();
271}
272
273QStyleHints *QQuickApplication::styleHints()
274{
275 return QGuiApplication::styleHints();
276}
277
278void QQuickApplication::setDisplayName(const QString &displayName)
279{
280 return QGuiApplication::setApplicationDisplayName(displayName);
281}
282
283QQmlListProperty<QQuickScreenInfo> QQuickApplication::screens()
284{
285 using screens_type = decltype(m_screens);
286
287 auto screens_count = [](QQmlListProperty<QQuickScreenInfo> *prop) -> qsizetype
288 {
289 return static_cast<screens_type *>(prop->data)->size();
290 };
291
292 auto screens_at = [](QQmlListProperty<QQuickScreenInfo> *prop, qsizetype idx) -> QQuickScreenInfo *
293 {
294 return static_cast<screens_type *>(prop->data)->at(idx).get();
295 };
296
297 return QQmlListProperty<QQuickScreenInfo>(this, &m_screens, screens_count, screens_at);
298}
299
300void QQuickApplication::updateScreens()
301{
302 const QList<QScreen *> screenList = QGuiApplication::screens();
303
304 const qsizetype oldSize = m_screens.size();
305 const qsizetype newSize = screenList.size();
306
307 m_screens.resize(newSize);
308
309 for (qsizetype i = oldSize; i < newSize; ++i) {
310 // initialize new elements
311 m_screens[i] = std::make_unique<QQuickScreenInfo>(this);
312 }
313
314 for (qsizetype i = 0; i < newSize; ++i)
315 m_screens[i]->setWrappedScreen(screenList[i]);
316
317 emit screensChanged();
318}
319
320QT_END_NAMESPACE
321
322#include "moc_qquickapplication_p.cpp"