summaryrefslogtreecommitdiffstats
path: root/src/gui/kernel/qguiapplication.cpp
diff options
context:
space:
mode:
authorFriedemann Kleint <Friedemann.Kleint@theqtcompany.com>2015-02-24 09:35:56 +0100
committerFriedemann Kleint <Friedemann.Kleint@theqtcompany.com>2015-02-27 06:02:44 +0000
commit175b12beba7bb4d1bf150c217afffc3d88801272 (patch)
treea81b924c20f9e911778f7334b2f6b76fb1a70600 /src/gui/kernel/qguiapplication.cpp
parent4e966497ce3674286f38faab68679201ec6ca22e (diff)
Prevent static functions of Q[Gui]Application from crashing if there is no instance.
Add tests. Task-number: QTBUG-44499 Change-Id: I160b089ad3f23ab71a87519e50f8a2ef5d2a4a6f Reviewed-by: Marc Mutz <marc.mutz@kdab.com>
Diffstat (limited to 'src/gui/kernel/qguiapplication.cpp')
-rw-r--r--src/gui/kernel/qguiapplication.cpp15
1 files changed, 15 insertions, 0 deletions
diff --git a/src/gui/kernel/qguiapplication.cpp b/src/gui/kernel/qguiapplication.cpp
index d334bb72fa..b60ef4b8c1 100644
--- a/src/gui/kernel/qguiapplication.cpp
+++ b/src/gui/kernel/qguiapplication.cpp
@@ -105,6 +105,14 @@
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_GUI_EXPORT bool qt_is_gui_used = true;
Qt::MouseButtons QGuiApplicationPrivate::mouse_buttons = Qt::NoButton;
@@ -648,6 +656,7 @@ QString QGuiApplication::applicationDisplayName()
*/
QWindow *QGuiApplication::modalWindow()
{
+ CHECK_QAPP_INSTANCE(Q_NULLPTR)
if (QGuiApplicationPrivate::self->modalWindowList.isEmpty())
return 0;
return QGuiApplicationPrivate::self->modalWindowList.first();
@@ -1430,6 +1439,7 @@ Qt::KeyboardModifiers QGuiApplication::keyboardModifiers()
*/
Qt::KeyboardModifiers QGuiApplication::queryKeyboardModifiers()
{
+ CHECK_QAPP_INSTANCE(Qt::KeyboardModifiers(0))
QPlatformIntegration *pi = QGuiApplicationPrivate::platformIntegration();
return pi->queryKeyboardModifiers();
}
@@ -3167,6 +3177,7 @@ Qt::LayoutDirection QGuiApplication::layoutDirection()
#ifndef QT_NO_CURSOR
QCursor *QGuiApplication::overrideCursor()
{
+ CHECK_QAPP_INSTANCE(Q_NULLPTR)
return qGuiApp->d_func()->cursor_list.isEmpty() ? 0 : &qGuiApp->d_func()->cursor_list.first();
}
@@ -3180,6 +3191,7 @@ QCursor *QGuiApplication::overrideCursor()
*/
void QGuiApplication::changeOverrideCursor(const QCursor &cursor)
{
+ CHECK_QAPP_INSTANCE()
if (qGuiApp->d_func()->cursor_list.isEmpty())
return;
qGuiApp->d_func()->cursor_list.removeFirst();
@@ -3254,6 +3266,7 @@ static inline void applyWindowCursor(const QList<QWindow *> &l)
*/
void QGuiApplication::setOverrideCursor(const QCursor &cursor)
{
+ CHECK_QAPP_INSTANCE()
qGuiApp->d_func()->cursor_list.prepend(cursor);
applyCursor(QGuiApplicationPrivate::window_list, cursor);
}
@@ -3271,6 +3284,7 @@ void QGuiApplication::setOverrideCursor(const QCursor &cursor)
*/
void QGuiApplication::restoreOverrideCursor()
{
+ CHECK_QAPP_INSTANCE()
if (qGuiApp->d_func()->cursor_list.isEmpty())
return;
qGuiApp->d_func()->cursor_list.removeFirst();
@@ -3338,6 +3352,7 @@ bool QGuiApplication::desktopSettingsAware()
*/
QInputMethod *QGuiApplication::inputMethod()
{
+ CHECK_QAPP_INSTANCE(Q_NULLPTR)
if (!qGuiApp->d_func()->inputMethod)
qGuiApp->d_func()->inputMethod = new QInputMethod();
return qGuiApp->d_func()->inputMethod;