-
Notifications
You must be signed in to change notification settings - Fork 1k
/
qguiapplication.cpp
4311 lines (3608 loc) · 158 KB
/
qguiapplication.cpp
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/****************************************************************************
**
** Copyright (C) 2020 The Qt Company Ltd.
** Copyright (C) 2016 Intel Corporation.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtGui module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "qguiapplication.h"
#include "private/qguiapplication_p.h"
#include <qpa/qplatformintegrationfactory_p.h>
#include "private/qevent_p.h"
#include "qfont.h"
#include "qpointingdevice.h"
#include <qpa/qplatformfontdatabase.h>
#include <qpa/qplatformwindow.h>
#include <qpa/qplatformnativeinterface.h>
#include <qpa/qplatformtheme.h>
#include <qpa/qplatformintegration.h>
#include <QtCore/QAbstractEventDispatcher>
#include <QtCore/QStandardPaths>
#include <QtCore/QVariant>
#include <QtCore/private/qcoreapplication_p.h>
#include <QtCore/private/qabstracteventdispatcher_p.h>
#include <QtCore/qmutex.h>
#include <QtCore/private/qthread_p.h>
#include <QtCore/private/qlocking_p.h>
#include <QtCore/qdir.h>
#include <QtCore/qlibraryinfo.h>
#include <QtCore/qnumeric.h>
#include <QtDebug>
#ifndef QT_NO_ACCESSIBILITY
#include "qaccessible.h"
#endif
#include <qpalette.h>
#include <qscreen.h>
#include "qsessionmanager.h"
#include <private/qcolortrclut_p.h>
#include <private/qscreen_p.h>
#include <QtGui/qgenericpluginfactory.h>
#include <QtGui/qstylehints.h>
#include <QtGui/qinputmethod.h>
#include <QtGui/qpixmapcache.h>
#include <qpa/qplatforminputcontext.h>
#include <qpa/qplatforminputcontext_p.h>
#include <qpa/qwindowsysteminterface.h>
#include <qpa/qwindowsysteminterface_p.h>
#include "private/qwindow_p.h"
#include "private/qcursor_p.h"
#include "private/qopenglcontext_p.h"
#include "private/qinputdevicemanager_p.h"
#include "private/qinputmethod_p.h"
#include "private/qpointingdevice_p.h"
#include <qpa/qplatformthemefactory_p.h>
#if QT_CONFIG(draganddrop)
#include <qpa/qplatformdrag.h>
#include <private/qdnd_p.h>
#endif
#ifndef QT_NO_CURSOR
#include <qpa/qplatformcursor.h>
#endif
#include <QtGui/QPixmap>
#ifndef QT_NO_CLIPBOARD
#include <QtGui/QClipboard>
#endif
#if QT_CONFIG(library)
#include <QtCore/QLibrary>
#endif
#if defined(Q_OS_MAC)
# include "private/qcore_mac_p.h"
#elif defined(Q_OS_WIN)
# include <QtCore/qt_windows.h>
# include <QtCore/QLibraryInfo>
#endif // Q_OS_WIN
#ifdef Q_OS_WASM
#include <emscripten.h>
#endif
#include <qtgui_tracepoints_p.h>
#include <ctype.h>
QT_BEGIN_NAMESPACE
// Helper macro for static functions to check on the existence of the application class.
#define CHECK_QAPP_INSTANCE(...) \
if (Q_LIKELY(QCoreApplication::instance())) { \
} else { \
qWarning("Must construct a QGuiApplication first."); \
return __VA_ARGS__; \
}
Q_CORE_EXPORT void qt_call_post_routines();
Q_GUI_EXPORT bool qt_is_gui_used = true;
Qt::MouseButtons QGuiApplicationPrivate::mouse_buttons = Qt::NoButton;
Qt::KeyboardModifiers QGuiApplicationPrivate::modifier_buttons = Qt::NoModifier;
QPointF QGuiApplicationPrivate::lastCursorPosition(qInf(), qInf());
QWindow *QGuiApplicationPrivate::currentMouseWindow = nullptr;
QString QGuiApplicationPrivate::styleOverride;
Qt::ApplicationState QGuiApplicationPrivate::applicationState = Qt::ApplicationInactive;
Qt::HighDpiScaleFactorRoundingPolicy QGuiApplicationPrivate::highDpiScaleFactorRoundingPolicy =
#ifdef Q_OS_ANDROID
// On Android, Qt has newer rounded the scale factor. Preserve
// that behavior by disabling rounding by default.
Qt::HighDpiScaleFactorRoundingPolicy::PassThrough;
#else
Qt::HighDpiScaleFactorRoundingPolicy::Round;
#endif
bool QGuiApplicationPrivate::highDpiScalingUpdated = false;
QPointer<QWindow> QGuiApplicationPrivate::currentDragWindow;
QVector<QGuiApplicationPrivate::TabletPointData> QGuiApplicationPrivate::tabletDevicePoints;
QPlatformIntegration *QGuiApplicationPrivate::platform_integration = nullptr;
QPlatformTheme *QGuiApplicationPrivate::platform_theme = nullptr;
QList<QObject *> QGuiApplicationPrivate::generic_plugin_list;
#ifndef QT_NO_SESSIONMANAGER
bool QGuiApplicationPrivate::is_fallback_session_management_enabled = true;
#endif
enum ApplicationResourceFlags
{
ApplicationFontExplicitlySet = 0x2
};
static unsigned applicationResourceFlags = 0;
QIcon *QGuiApplicationPrivate::app_icon = nullptr;
QString *QGuiApplicationPrivate::platform_name = nullptr;
QString *QGuiApplicationPrivate::displayName = nullptr;
QString *QGuiApplicationPrivate::desktopFileName = nullptr;
QPalette *QGuiApplicationPrivate::app_pal = nullptr; // default application palette
ulong QGuiApplicationPrivate::mousePressTime = 0;
Qt::MouseButton QGuiApplicationPrivate::mousePressButton = Qt::NoButton;
int QGuiApplicationPrivate::mousePressX = 0;
int QGuiApplicationPrivate::mousePressY = 0;
static int mouseDoubleClickDistance = -1;
static int touchDoubleTapDistance = -1;
QWindow *QGuiApplicationPrivate::currentMousePressWindow = nullptr;
static Qt::LayoutDirection layout_direction = Qt::LayoutDirectionAuto;
static bool force_reverse = false;
QGuiApplicationPrivate *QGuiApplicationPrivate::self = nullptr;
QPointingDevice *QGuiApplicationPrivate::m_fakeTouchDevice = nullptr;
int QGuiApplicationPrivate::m_fakeMouseSourcePointId = 0;
#ifndef QT_NO_CLIPBOARD
QClipboard *QGuiApplicationPrivate::qt_clipboard = nullptr;
#endif
QList<QScreen *> QGuiApplicationPrivate::screen_list;
QWindowList QGuiApplicationPrivate::window_list;
QWindow *QGuiApplicationPrivate::focus_window = nullptr;
static QBasicMutex applicationFontMutex;
QFont *QGuiApplicationPrivate::app_font = nullptr;
QStyleHints *QGuiApplicationPrivate::styleHints = nullptr;
bool QGuiApplicationPrivate::obey_desktop_settings = true;
QInputDeviceManager *QGuiApplicationPrivate::m_inputDeviceManager = nullptr;
qreal QGuiApplicationPrivate::m_maxDevicePixelRatio = 0.0;
static qreal fontSmoothingGamma = 1.7;
extern void qRegisterGuiVariant();
#if QT_CONFIG(animation)
extern void qRegisterGuiGetInterpolator();
#endif
static bool qt_detectRTLLanguage()
{
return force_reverse ^
(QGuiApplication::tr("QT_LAYOUT_DIRECTION",
"Translate this string to the string 'LTR' in left-to-right"
" languages or to 'RTL' in right-to-left languages (such as Hebrew"
" and Arabic) to get proper widget layout.") == QLatin1String("RTL"));
}
static void initFontUnlocked()
{
if (!QGuiApplicationPrivate::app_font) {
if (const QPlatformTheme *theme = QGuiApplicationPrivate::platformTheme())
if (const QFont *font = theme->font(QPlatformTheme::SystemFont))
QGuiApplicationPrivate::app_font = new QFont(*font);
}
if (!QGuiApplicationPrivate::app_font)
QGuiApplicationPrivate::app_font =
new QFont(QGuiApplicationPrivate::platformIntegration()->fontDatabase()->defaultFont());
}
static inline void clearFontUnlocked()
{
delete QGuiApplicationPrivate::app_font;
QGuiApplicationPrivate::app_font = nullptr;
}
static void initThemeHints()
{
mouseDoubleClickDistance = QGuiApplicationPrivate::platformTheme()->themeHint(QPlatformTheme::MouseDoubleClickDistance).toInt();
touchDoubleTapDistance = QGuiApplicationPrivate::platformTheme()->themeHint(QPlatformTheme::TouchDoubleTapDistance).toInt();
}
static bool checkNeedPortalSupport()
{
#if QT_CONFIG(dbus)
return !QStandardPaths::locate(QStandardPaths::RuntimeLocation, QLatin1String("flatpak-info")).isEmpty() || qEnvironmentVariableIsSet("SNAP");
#else
return false;
#endif // QT_CONFIG(dbus)
}
// Using aggregate initialization instead of ctor so we can have a POD global static
#define Q_WINDOW_GEOMETRY_SPECIFICATION_INITIALIZER { Qt::TopLeftCorner, -1, -1, -1, -1 }
// Geometry specification for top level windows following the convention of the
// -geometry command line arguments in X11 (see XParseGeometry).
struct QWindowGeometrySpecification
{
static QWindowGeometrySpecification fromArgument(const QByteArray &a);
void applyTo(QWindow *window) const;
Qt::Corner corner;
int xOffset;
int yOffset;
int width;
int height;
};
// Parse a token of a X11 geometry specification "200x100+10-20".
static inline int nextGeometryToken(const QByteArray &a, int &pos, char *op)
{
*op = 0;
const int size = a.size();
if (pos >= size)
return -1;
*op = a.at(pos);
if (*op == '+' || *op == '-' || *op == 'x')
pos++;
else if (isdigit(*op))
*op = 'x'; // If it starts with a digit, it is supposed to be a width specification.
else
return -1;
const int numberPos = pos;
for ( ; pos < size && isdigit(a.at(pos)); ++pos) ;
bool ok;
const int result = a.mid(numberPos, pos - numberPos).toInt(&ok);
return ok ? result : -1;
}
QWindowGeometrySpecification QWindowGeometrySpecification::fromArgument(const QByteArray &a)
{
QWindowGeometrySpecification result = Q_WINDOW_GEOMETRY_SPECIFICATION_INITIALIZER;
int pos = 0;
for (int i = 0; i < 4; ++i) {
char op;
const int value = nextGeometryToken(a, pos, &op);
if (value < 0)
break;
switch (op) {
case 'x':
(result.width >= 0 ? result.height : result.width) = value;
break;
case '+':
case '-':
if (result.xOffset >= 0) {
result.yOffset = value;
if (op == '-')
result.corner = result.corner == Qt::TopRightCorner ? Qt::BottomRightCorner : Qt::BottomLeftCorner;
} else {
result.xOffset = value;
if (op == '-')
result.corner = Qt::TopRightCorner;
}
}
}
return result;
}
void QWindowGeometrySpecification::applyTo(QWindow *window) const
{
QRect windowGeometry = window->frameGeometry();
QSize size = windowGeometry.size();
if (width >= 0 || height >= 0) {
const QSize windowMinimumSize = window->minimumSize();
const QSize windowMaximumSize = window->maximumSize();
if (width >= 0)
size.setWidth(qBound(windowMinimumSize.width(), width, windowMaximumSize.width()));
if (height >= 0)
size.setHeight(qBound(windowMinimumSize.height(), height, windowMaximumSize.height()));
window->resize(size);
}
if (xOffset >= 0 || yOffset >= 0) {
const QRect availableGeometry = window->screen()->virtualGeometry();
QPoint topLeft = windowGeometry.topLeft();
if (xOffset >= 0) {
topLeft.setX(corner == Qt::TopLeftCorner || corner == Qt::BottomLeftCorner ?
xOffset :
qMax(availableGeometry.right() - size.width() - xOffset, availableGeometry.left()));
}
if (yOffset >= 0) {
topLeft.setY(corner == Qt::TopLeftCorner || corner == Qt::TopRightCorner ?
yOffset :
qMax(availableGeometry.bottom() - size.height() - yOffset, availableGeometry.top()));
}
window->setFramePosition(topLeft);
}
}
static QWindowGeometrySpecification windowGeometrySpecification = Q_WINDOW_GEOMETRY_SPECIFICATION_INITIALIZER;
/*!
\macro qGuiApp
\relates QGuiApplication
A global pointer referring to the unique application object.
Only valid for use when that object is a QGuiApplication.
\sa QCoreApplication::instance(), qApp
*/
/*!
\class QGuiApplication
\brief The QGuiApplication class manages the GUI application's control
flow and main settings.
\inmodule QtGui
\since 5.0
QGuiApplication contains the main event loop, where all events from the window
system and other sources are processed and dispatched. It also handles the
application's initialization and finalization, and provides session management.
In addition, QGuiApplication handles most of the system-wide and application-wide
settings.
For any GUI application using Qt, there is precisely \b one QGuiApplication
object no matter whether the application has 0, 1, 2 or more windows at
any given time. For non-GUI Qt applications, use QCoreApplication instead,
as it does not depend on the Qt GUI module. For QWidget based Qt applications,
use QApplication instead, as it provides some functionality needed for creating
QWidget instances.
The QGuiApplication object is accessible through the instance() function, which
returns a pointer equivalent to the global \l qApp pointer.
QGuiApplication's main areas of responsibility are:
\list
\li It initializes the application with the user's desktop settings,
such as palette(), font() and styleHints(). It keeps
track of these properties in case the user changes the desktop
globally, for example, through some kind of control panel.
\li It performs event handling, meaning that it receives events
from the underlying window system and dispatches them to the
relevant widgets. You can send your own events to windows by
using sendEvent() and postEvent().
\li It parses common command line arguments and sets its internal
state accordingly. See the \l{QGuiApplication::QGuiApplication()}
{constructor documentation} below for more details.
\li It provides localization of strings that are visible to the
user via translate().
\li It provides some magical objects like the clipboard().
\li It knows about the application's windows. You can ask which
window is at a certain position using topLevelAt(), get a list of
topLevelWindows(), etc.
\li It manages the application's mouse cursor handling, see
setOverrideCursor()
\li It provides support for sophisticated \l{Session Management}
{session management}. This makes it possible for applications
to terminate gracefully when the user logs out, to cancel a
shutdown process if termination isn't possible and even to
preserve the entire application's state for a future session.
See isSessionRestored(), sessionId() and commitDataRequest() and
saveStateRequest() for details.
\endlist
Since the QGuiApplication object does so much initialization, it \e{must} be
created before any other objects related to the user interface are created.
QGuiApplication also deals with common command line arguments. Hence, it is
usually a good idea to create it \e before any interpretation or
modification of \c argv is done in the application itself.
\table
\header
\li{2,1} Groups of functions
\row
\li System settings
\li desktopSettingsAware(),
setDesktopSettingsAware(),
styleHints(),
palette(),
setPalette(),
font(),
setFont().
\row
\li Event handling
\li exec(),
processEvents(),
exit(),
quit().
sendEvent(),
postEvent(),
sendPostedEvents(),
removePostedEvents(),
hasPendingEvents(),
notify().
\row
\li Windows
\li allWindows(),
topLevelWindows(),
focusWindow(),
clipboard(),
topLevelAt().
\row
\li Advanced cursor handling
\li overrideCursor(),
setOverrideCursor(),
restoreOverrideCursor().
\row
\li Session management
\li isSessionRestored(),
sessionId(),
commitDataRequest(),
saveStateRequest().
\row
\li Miscellaneous
\li startingUp(),
closingDown().
\endtable
\sa QCoreApplication, QAbstractEventDispatcher, QEventLoop
*/
/*!
Initializes the window system and constructs an application object with
\a argc command line arguments in \a argv.
\warning The data referred to by \a argc and \a argv must stay valid for
the entire lifetime of the QGuiApplication object. In addition, \a argc must
be greater than zero and \a argv must contain at least one valid character
string.
The global \c qApp pointer refers to this application object. Only one
application object should be created.
This application object must be constructed before any \l{QPaintDevice}
{paint devices} (including pixmaps, bitmaps etc.).
\note \a argc and \a argv might be changed as Qt removes command line
arguments that it recognizes.
\section1 Supported Command Line Options
All Qt programs automatically support a set of command-line options that
allow modifying the way Qt will interact with the windowing system. Some of
the options are also accessible via environment variables, which are the
preferred form if the application can launch GUI sub-processes or other
applications (environment variables will be inherited by child processes).
When in doubt, use the environment variables.
The options currently supported are the following:
\list
\li \c{-platform} \e {platformName[:options]}, specifies the
\l{Qt Platform Abstraction} (QPA) plugin.
Overrides the \c QT_QPA_PLATFORM environment variable.
\li \c{-platformpluginpath} \e path, specifies the path to platform
plugins.
Overrides the \c QT_QPA_PLATFORM_PLUGIN_PATH environment variable.
\li \c{-platformtheme} \e platformTheme, specifies the platform theme.
Overrides the \c QT_QPA_PLATFORMTHEME environment variable.
\li \c{-plugin} \e plugin, specifies additional plugins to load. The argument
may appear multiple times.
Concatenated with the plugins in the \c QT_QPA_GENERIC_PLUGINS environment
variable.
\li \c{-qmljsdebugger=}, activates the QML/JS debugger with a specified port.
The value must be of format \c{port:1234}\e{[,block]}, where
\e block is optional
and will make the application wait until a debugger connects to it.
\li \c {-qwindowgeometry} \e geometry, specifies window geometry for
the main window using the X11-syntax. For example:
\c {-qwindowgeometry 100x100+50+50}
\li \c {-qwindowicon}, sets the default window icon
\li \c {-qwindowtitle}, sets the title of the first window
\li \c{-reverse}, sets the application's layout direction to
Qt::RightToLeft. This option is intended to aid debugging and should
not be used in production. The default value is automatically detected
from the user's locale (see also QLocale::textDirection()).
\li \c{-session} \e session, restores the application from an earlier
\l{Session Management}{session}.
\endlist
The following standard command line options are available for X11:
\list
\li \c {-display} \e {hostname:screen_number}, switches displays on X11.
Overrides the \c DISPLAY environment variable.
\li \c {-geometry} \e geometry, same as \c {-qwindowgeometry}.
\endlist
\section1 Platform-Specific Arguments
You can specify platform-specific arguments for the \c{-platform} option.
Place them after the platform plugin name following a colon as a
comma-separated list. For example,
\c{-platform windows:dialogs=xp,fontengine=freetype}.
The following parameters are available for \c {-platform windows}:
\list
\li \c {altgr}, detect the key \c {AltGr} found on some keyboards as
Qt::GroupSwitchModifier (since Qt 5.12).
\li \c {darkmode=[1|2]} controls how Qt responds to the activation
of the \e{Dark Mode for applications} introduced in Windows 10
1903 (since Qt 5.15).
A value of 1 causes Qt to switch the window borders to black
when \e{Dark Mode for applications} is activated and no High
Contrast Theme is in use. This is intended for applications
that implement their own theming.
A value of 2 will in addition cause the Windows Vista style to
be deactivated and switch to the Windows style using a
simplified palette in dark mode. This is currently
experimental pending the introduction of new style that
properly adapts to dark mode.
\li \c {dialogs=[xp|none]}, \c xp uses XP-style native dialogs and
\c none disables them.
\li \c {dpiawareness=[0|1|2]} Sets the DPI awareness of the process
(see \l{High DPI Displays}, since Qt 5.4).
\li \c {fontengine=freetype}, uses the FreeType font engine.
\li \c {fontengine=directwrite}, uses the experimental DirectWrite
font database and defaults to using the DirectWrite font
engine (which is otherwise only used for some font types
or font properties.) This affects font selection and aims
to provide font naming more consistent with other platforms,
but does not support all font formats, such as Postscript
Type-1 or Microsoft FNT fonts.
\li \c {menus=[native|none]}, controls the use of native menus.
Native menus are implemented using Win32 API and are simpler than
QMenu-based menus in for example that they do allow for placing
widgets on them or changing properties like fonts and do not
provide hover signals. They are mainly intended for Qt Quick.
By default, they will be used if the application is not an
instance of QApplication or for Qt Quick Controls 2
applications (since Qt 5.10).
\li \c {nocolorfonts} Turn off DirectWrite Color fonts
(since Qt 5.8).
\li \c {nodirectwrite} Turn off DirectWrite fonts (since Qt 5.8).
\li \c {nomousefromtouch} Ignores mouse events synthesized
from touch events by the operating system.
\li \c {nowmpointer} Switches from Pointer Input Messages handling
to legacy mouse handling (since Qt 5.12).
\li \c {reverse} Activates Right-to-left mode (experimental).
Windows title bars will be shown accordingly in Right-to-left locales
(since Qt 5.13).
\li \c {tabletabsoluterange=<value>} Sets a value for mouse mode detection
of WinTab tablets (Legacy, since Qt 5.3).
\endlist
The following parameter is available for \c {-platform cocoa} (on macOS):
\list
\li \c {fontengine=freetype}, uses the FreeType font engine.
\endlist
For more information about the platform-specific arguments available for
embedded Linux platforms, see \l{Qt for Embedded Linux}.
\sa arguments() QGuiApplication::platformName
*/
#ifdef Q_QDOC
QGuiApplication::QGuiApplication(int &argc, char **argv)
#else
QGuiApplication::QGuiApplication(int &argc, char **argv, int flags)
#endif
: QCoreApplication(*new QGuiApplicationPrivate(argc, argv, flags))
{
d_func()->init();
QCoreApplicationPrivate::eventDispatcher->startingUp();
}
/*!
\internal
*/
QGuiApplication::QGuiApplication(QGuiApplicationPrivate &p)
: QCoreApplication(p)
{
}
/*!
Destructs the application.
*/
QGuiApplication::~QGuiApplication()
{
Q_D(QGuiApplication);
qt_call_post_routines();
d->eventDispatcher->closingDown();
d->eventDispatcher = nullptr;
#ifndef QT_NO_CLIPBOARD
delete QGuiApplicationPrivate::qt_clipboard;
QGuiApplicationPrivate::qt_clipboard = nullptr;
#endif
#ifndef QT_NO_SESSIONMANAGER
delete d->session_manager;
d->session_manager = nullptr;
#endif //QT_NO_SESSIONMANAGER
QGuiApplicationPrivate::clearPalette();
QFontDatabase::removeAllApplicationFonts();
#ifndef QT_NO_CURSOR
d->cursor_list.clear();
#endif
delete QGuiApplicationPrivate::app_icon;
QGuiApplicationPrivate::app_icon = nullptr;
delete QGuiApplicationPrivate::platform_name;
QGuiApplicationPrivate::platform_name = nullptr;
delete QGuiApplicationPrivate::displayName;
QGuiApplicationPrivate::displayName = nullptr;
delete QGuiApplicationPrivate::m_inputDeviceManager;
QGuiApplicationPrivate::m_inputDeviceManager = nullptr;
delete QGuiApplicationPrivate::desktopFileName;
QGuiApplicationPrivate::desktopFileName = nullptr;
QGuiApplicationPrivate::mouse_buttons = Qt::NoButton;
QGuiApplicationPrivate::modifier_buttons = Qt::NoModifier;
QGuiApplicationPrivate::lastCursorPosition = {qInf(), qInf()};
QGuiApplicationPrivate::currentMousePressWindow = QGuiApplicationPrivate::currentMouseWindow = nullptr;
QGuiApplicationPrivate::applicationState = Qt::ApplicationInactive;
QGuiApplicationPrivate::highDpiScalingUpdated = false;
QGuiApplicationPrivate::currentDragWindow = nullptr;
QGuiApplicationPrivate::tabletDevicePoints.clear();
#ifndef QT_NO_SESSIONMANAGER
QGuiApplicationPrivate::is_fallback_session_management_enabled = true;
#endif
QGuiApplicationPrivate::mousePressTime = 0;
QGuiApplicationPrivate::mousePressX = QGuiApplicationPrivate::mousePressY = 0;
}
QGuiApplicationPrivate::QGuiApplicationPrivate(int &argc, char **argv, int flags)
: QCoreApplicationPrivate(argc, argv, flags),
inputMethod(nullptr),
lastTouchType(QEvent::TouchEnd),
ownGlobalShareContext(false)
{
self = this;
application_type = QCoreApplicationPrivate::Gui;
#ifndef QT_NO_SESSIONMANAGER
is_session_restored = false;
is_saving_session = false;
#endif
}
/*!
\property QGuiApplication::applicationDisplayName
\brief the user-visible name of this application
\since 5.0
This name is shown to the user, for instance in window titles.
It can be translated, if necessary.
If not set, the application display name defaults to the application name.
\sa applicationName
*/
void QGuiApplication::setApplicationDisplayName(const QString &name)
{
if (!QGuiApplicationPrivate::displayName) {
QGuiApplicationPrivate::displayName = new QString(name);
if (qGuiApp) {
disconnect(qGuiApp, &QGuiApplication::applicationNameChanged,
qGuiApp, &QGuiApplication::applicationDisplayNameChanged);
if (*QGuiApplicationPrivate::displayName != applicationName())
emit qGuiApp->applicationDisplayNameChanged();
}
} else if (name != *QGuiApplicationPrivate::displayName) {
*QGuiApplicationPrivate::displayName = name;
if (qGuiApp)
emit qGuiApp->applicationDisplayNameChanged();
}
}
QString QGuiApplication::applicationDisplayName()
{
return QGuiApplicationPrivate::displayName ? *QGuiApplicationPrivate::displayName : applicationName();
}
/*!
\property QGuiApplication::desktopFileName
\brief the base name of the desktop entry for this application
\since 5.7
This is the file name, without the full path, of the desktop entry
that represents this application according to the freedesktop desktop
entry specification.
This property gives a precise indication of what desktop entry represents
the application and it is needed by the windowing system to retrieve
such information without resorting to imprecise heuristics.
The latest version of the freedesktop desktop entry specification can be obtained
\l{http://standards.freedesktop.org/desktop-entry-spec/latest/}{here}.
*/
void QGuiApplication::setDesktopFileName(const QString &name)
{
if (!QGuiApplicationPrivate::desktopFileName)
QGuiApplicationPrivate::desktopFileName = new QString;
*QGuiApplicationPrivate::desktopFileName = name;
}
QString QGuiApplication::desktopFileName()
{
return QGuiApplicationPrivate::desktopFileName ? *QGuiApplicationPrivate::desktopFileName : QString();
}
/*!
Returns the most recently shown modal window. If no modal windows are
visible, this function returns zero.
A modal window is a window which has its
\l{QWindow::modality}{modality} property set to Qt::WindowModal
or Qt::ApplicationModal. A modal window must be closed before the user can
continue with other parts of the program.
Modal window are organized in a stack. This function returns the modal
window at the top of the stack.
\sa Qt::WindowModality, QWindow::setModality()
*/
QWindow *QGuiApplication::modalWindow()
{
CHECK_QAPP_INSTANCE(nullptr)
if (QGuiApplicationPrivate::self->modalWindowList.isEmpty())
return nullptr;
return QGuiApplicationPrivate::self->modalWindowList.first();
}
static void updateBlockedStatusRecursion(QWindow *window, bool shouldBeBlocked)
{
QWindowPrivate *p = qt_window_private(window);
if (p->blockedByModalWindow != shouldBeBlocked) {
p->blockedByModalWindow = shouldBeBlocked;
QEvent e(shouldBeBlocked ? QEvent::WindowBlocked : QEvent::WindowUnblocked);
QGuiApplication::sendEvent(window, &e);
for (QObject *c : window->children()) {
if (c->isWindowType())
updateBlockedStatusRecursion(static_cast<QWindow *>(c), shouldBeBlocked);
}
}
}
void QGuiApplicationPrivate::updateBlockedStatus(QWindow *window)
{
bool shouldBeBlocked = false;
const bool popupType = (window->type() == Qt::ToolTip) || (window->type() == Qt::Popup);
if (!popupType && !self->modalWindowList.isEmpty())
shouldBeBlocked = self->isWindowBlocked(window);
updateBlockedStatusRecursion(window, shouldBeBlocked);
}
// Return whether the window needs to be notified about window blocked events.
// As opposed to QGuiApplication::topLevelWindows(), embedded windows are
// included in this list (QTBUG-18099).
static inline bool needsWindowBlockedEvent(const QWindow *w)
{
return w->isTopLevel() && w->type() != Qt::Desktop;
}
void QGuiApplicationPrivate::showModalWindow(QWindow *modal)
{
self->modalWindowList.prepend(modal);
// Send leave for currently entered window if it should be blocked
if (currentMouseWindow && !QWindowPrivate::get(currentMouseWindow)->isPopup()) {
bool shouldBeBlocked = self->isWindowBlocked(currentMouseWindow);
if (shouldBeBlocked) {
// Remove the new window from modalWindowList temporarily so leave can go through
self->modalWindowList.removeFirst();
QEvent e(QEvent::Leave);
QGuiApplication::sendEvent(currentMouseWindow, &e);
currentMouseWindow = nullptr;
self->modalWindowList.prepend(modal);
}
}
for (QWindow *window : qAsConst(QGuiApplicationPrivate::window_list)) {
if (needsWindowBlockedEvent(window) && !window->d_func()->blockedByModalWindow)
updateBlockedStatus(window);
}
updateBlockedStatus(modal);
}
void QGuiApplicationPrivate::hideModalWindow(QWindow *window)
{
self->modalWindowList.removeAll(window);
for (QWindow *window : qAsConst(QGuiApplicationPrivate::window_list)) {
if (needsWindowBlockedEvent(window) && window->d_func()->blockedByModalWindow)
updateBlockedStatus(window);
}
}
/*
Returns \c true if \a window is blocked by a modal window. If \a
blockingWindow is non-zero, *blockingWindow will be set to the blocking
window (or to zero if \a window is not blocked).
*/
bool QGuiApplicationPrivate::isWindowBlocked(QWindow *window, QWindow **blockingWindow) const
{
QWindow *unused = nullptr;
if (!blockingWindow)
blockingWindow = &unused;
if (modalWindowList.isEmpty()) {
*blockingWindow = nullptr;
return false;
}
for (int i = 0; i < modalWindowList.count(); ++i) {
QWindow *modalWindow = modalWindowList.at(i);
// A window is not blocked by another modal window if the two are
// the same, or if the window is a child of the modal window.
if (window == modalWindow || modalWindow->isAncestorOf(window, QWindow::IncludeTransients)) {
*blockingWindow = nullptr;
return false;
}
Qt::WindowModality windowModality = modalWindow->modality();
switch (windowModality) {
case Qt::ApplicationModal:
{
if (modalWindow != window) {
*blockingWindow = modalWindow;
return true;
}
break;
}
case Qt::WindowModal:
{
QWindow *w = window;
do {
QWindow *m = modalWindow;
do {
if (m == w) {
*blockingWindow = m;
return true;
}
QWindow *p = m->parent();
if (!p)
p = m->transientParent();
m = p;
} while (m);
QWindow *p = w->parent();
if (!p)
p = w->transientParent();
w = p;
} while (w);
break;
}
default:
Q_ASSERT_X(false, "QGuiApplication", "internal error, a modal widget cannot be modeless");
break;
}
}
*blockingWindow = nullptr;
return false;
}
/*!
Returns the QWindow that receives events tied to focus,
such as key events.
*/
QWindow *QGuiApplication::focusWindow()
{
return QGuiApplicationPrivate::focus_window;
}
/*!
\fn QGuiApplication::focusObjectChanged(QObject *focusObject)
This signal is emitted when final receiver of events tied to focus is changed.
\a focusObject is the new receiver.
\sa focusObject()
*/
/*!
\fn QGuiApplication::focusWindowChanged(QWindow *focusWindow)
This signal is emitted when the focused window changes.
\a focusWindow is the new focused window.
\sa focusWindow()
*/
/*!
Returns the QObject in currently active window that will be final receiver of events
tied to focus, such as key events.
*/
QObject *QGuiApplication::focusObject()
{