From 4fbfbf7fe8e6f8f31b5704a0f8f55415a7284523 Mon Sep 17 00:00:00 2001 From: Cavit Sina Dogru Date: Wed, 3 Aug 2016 17:54:26 +0300 Subject: Qt object: Add exit(int) This is similar to the Qt.quit() function but also specifies the return code that the event loop will return. [ChangeLog][QtQml] Added exit(int retCode) method to the Qt global object. An application can call Qt.exit to specify a return code of the engine. Task-number: QTBUG-54360 Change-Id: Iaa319e6dc4d6b99dc3a5c01845e87b936fd2cab0 Reviewed-by: Shawn Rutledge --- src/qml/qml/qqmlapplicationengine.cpp | 1 + src/qml/qml/qqmlengine.cpp | 19 +++++++++++++++++++ src/qml/qml/qqmlengine.h | 1 + src/qml/qml/qqmlengine_p.h | 1 + src/qml/qml/v8/qqmlbuiltinfunctions.cpp | 25 +++++++++++++++++++++++++ src/qml/qml/v8/qqmlbuiltinfunctions_p.h | 1 + 6 files changed, 48 insertions(+) (limited to 'src') diff --git a/src/qml/qml/qqmlapplicationengine.cpp b/src/qml/qml/qqmlapplicationengine.cpp index 30fc186391..fef2da753b 100644 --- a/src/qml/qml/qqmlapplicationengine.cpp +++ b/src/qml/qml/qqmlapplicationengine.cpp @@ -69,6 +69,7 @@ void QQmlApplicationEnginePrivate::init() q->connect(&statusMapper, SIGNAL(mapped(QObject*)), q, SLOT(_q_finishLoad(QObject*))); q->connect(q, SIGNAL(quit()), QCoreApplication::instance(), SLOT(quit())); + q->connect(q, &QQmlApplicationEngine::exit, QCoreApplication::instance(), &QCoreApplication::exit); #ifndef QT_NO_TRANSLATION QTranslator* qtTranslator = new QTranslator; if (qtTranslator->load(QLatin1String("qt_") + QLocale::system().name(), QLibraryInfo::location(QLibraryInfo::TranslationsPath))) diff --git a/src/qml/qml/qqmlengine.cpp b/src/qml/qml/qqmlengine.cpp index 2b49eae4de..cdaa9e83e9 100644 --- a/src/qml/qml/qqmlengine.cpp +++ b/src/qml/qml/qqmlengine.cpp @@ -982,8 +982,19 @@ QQmlEngine::~QQmlEngine() /*! \fn void QQmlEngine::quit() This signal is emitted when the QML loaded by the engine would like to quit. + + \sa exit() */ +/*! \fn void QQmlEngine::exit(int retCode) + This signal is emitted when the QML loaded by the engine would like to exit + from the event loop with the specified return code. + + \since 5.8 + \sa quit() + */ + + /*! \fn void QQmlEngine::warnings(const QList &warnings) This signal is emitted when \a warnings messages are generated by QML. */ @@ -1837,6 +1848,14 @@ void QQmlEnginePrivate::sendQuit() } } +void QQmlEnginePrivate::sendExit(int retCode) +{ + Q_Q(QQmlEngine); + if (q->receivers(SIGNAL(exit(int))) == 0) + qWarning("Signal QQmlEngine::exit() emitted, but no receivers connected to handle it."); + emit q->exit(retCode); +} + static void dumpwarning(const QQmlError &error) { QMessageLogger(error.url().toString().toLatin1().constData(), diff --git a/src/qml/qml/qqmlengine.h b/src/qml/qml/qqmlengine.h index 132af78f80..bbb9c36ce9 100644 --- a/src/qml/qml/qqmlengine.h +++ b/src/qml/qml/qqmlengine.h @@ -155,6 +155,7 @@ protected: Q_SIGNALS: void quit(); + void exit(int retCode); void warnings(const QList &warnings); private: diff --git a/src/qml/qml/qqmlengine_p.h b/src/qml/qml/qqmlengine_p.h index 949060f395..713b03dbf3 100644 --- a/src/qml/qml/qqmlengine_p.h +++ b/src/qml/qml/qqmlengine_p.h @@ -227,6 +227,7 @@ public: bool isScriptLoaded(const QUrl &url) const; void sendQuit(); + void sendExit(int retCode = 0); void warning(const QQmlError &); void warning(const QList &); void warning(QQmlDelayedError *); diff --git a/src/qml/qml/v8/qqmlbuiltinfunctions.cpp b/src/qml/qml/v8/qqmlbuiltinfunctions.cpp index 76a900c846..db0439ab9e 100644 --- a/src/qml/qml/v8/qqmlbuiltinfunctions.cpp +++ b/src/qml/qml/v8/qqmlbuiltinfunctions.cpp @@ -137,6 +137,7 @@ Heap::QtObject::QtObject(QQmlEngine *qmlEngine) o->defineDefaultProperty(QStringLiteral("darker"), QV4::QtObject::method_darker); o->defineDefaultProperty(QStringLiteral("tint"), QV4::QtObject::method_tint); o->defineDefaultProperty(QStringLiteral("quit"), QV4::QtObject::method_quit); + o->defineDefaultProperty(QStringLiteral("exit"), QV4::QtObject::method_exit); o->defineDefaultProperty(QStringLiteral("createQmlObject"), QV4::QtObject::method_createQmlObject); o->defineDefaultProperty(QStringLiteral("createComponent"), QV4::QtObject::method_createComponent); } @@ -992,6 +993,8 @@ This function causes the QQmlEngine::quit() signal to be emitted. Within the \l {Prototyping with qmlscene}, this causes the launcher application to exit; to quit a C++ application when this method is called, connect the QQmlEngine::quit() signal to the QCoreApplication::quit() slot. + +\sa exit() */ ReturnedValue QtObject::method_quit(CallContext *ctx) { @@ -999,6 +1002,28 @@ ReturnedValue QtObject::method_quit(CallContext *ctx) return QV4::Encode::undefined(); } +/*! + \qmlmethod Qt::exit(int retCode) + + This function causes the QQmlEngine::exit(int) signal to be emitted. + Within the \l {Prototyping with qmlscene}, this causes the launcher application to exit + the specified return code. To exit from the event loop with a specified return code when this + method is called, a C++ application can connect the QQmlEngine::exit(int) signal + to the QCoreApplication::exit(int) slot. + + \sa quit() +*/ +ReturnedValue QtObject::method_exit(CallContext *ctx) +{ + if (ctx->argc() != 1) + V4THROW_ERROR("Qt.exit(): Invalid arguments"); + + int retCode = ctx->args()[0].toNumber(); + + QQmlEnginePrivate::get(ctx->engine()->qmlEngine())->sendExit(retCode); + return QV4::Encode::undefined(); +} + /*! \qmlmethod object Qt::createQmlObject(string qml, object parent, string filepath) diff --git a/src/qml/qml/v8/qqmlbuiltinfunctions_p.h b/src/qml/qml/v8/qqmlbuiltinfunctions_p.h index d29983c476..8c0759679a 100644 --- a/src/qml/qml/v8/qqmlbuiltinfunctions_p.h +++ b/src/qml/qml/v8/qqmlbuiltinfunctions_p.h @@ -122,6 +122,7 @@ struct QtObject : Object static ReturnedValue method_btoa(CallContext *ctx); static ReturnedValue method_atob(CallContext *ctx); static ReturnedValue method_quit(CallContext *ctx); + static ReturnedValue method_exit(CallContext *ctx); static ReturnedValue method_resolvedUrl(CallContext *ctx); static ReturnedValue method_createQmlObject(CallContext *ctx); static ReturnedValue method_createComponent(CallContext *ctx); -- cgit v1.2.3