Solved Calling Qt.quit() during setup
-
Here's a test app.
main.cpp
is default boilerplate with quit() signal connected. I want to quit the application (from QML) as it is starting up.Without the Timer shown below, the application does not quit. With the Timer, it instantly quits as desired (before the window even shows up). Is there a cleaner way to ensure the quit() will work?
import QtQuick 2.8 import QtQuick.Window 2.2 Window { visible:true; width:640; height:480; color:'red'; title:'Quit Test' Component.onCompleted: Qt.quit() // Timer { interval:1; running:true; onTriggered:Qt.quit() } }
Motivation: I have a command-line-argument parser in QML; when it detects
--help
or-h
(duringComponent.onCompleted
), or when a required argument is omitted, it should print the help and quit the application. -
@Phrogz said in Calling Qt.quit() during setup:
Is there a cleaner way to ensure the quit() will work?
I don't believe so. You could use zero interval to queue it immediately in the event loop, beside that the code looks okay to me.
-
@progpow Have you checked the documentation for Qt.quit()?
Not sure how you're launching the QML application, but if you're using QQmlEngine from a C++ application, you might want to consider using the QCommandLineParser class to handle all the command line arguments there before even launching the QML part.
-
Hi,
Out of curiosity, why go all the way to the GUI when you have known option(s) that should basically not even trigger it ?
It would make more sense to use the QCommandLineParser class for options that should be handled outside your application GUI.
-
@SGaist It makes more sense to a C++ programmer for sure. I'm not a C++ programmer; I'm writing full applications in QML/JS with 12 lines of C++ provided by the new project template to keep me afloat. :)