I am developing a Qt 5.x application whose main function is more or less like that (simplified):
int main(int argc, char *argv[])
{
QApplication app(arg, argv);
QCommandLineParser cmdLineParser;
cmdLineParser.addHelpOption();
cmdLineParser.addVersionOption();
cmdLineParser.process(app);
/// *** SOME INITIALIZATION STUFF *** ///
app.exec();
}
I am experiencing some problems when I invoke the execution of application specifying the --version
(or --help
) option on command line: the application outputs the version (or the help) and then sometimes terminates with a segmentation fault...
I think it is due to the fact that when QCommandLineParser
detects a built-in option on command line (--version
or --help
) simply calls QApplication
's exit()
for terminating the execution and this interferes with my own *** SOME INITIALIZATION STUFF ***
(basically some other threads are started and maybe a prematurely call on exit()
causes some problems)...
However, my question is: is it possible to make Qt avoid to go further in my *** SOME INITIALIZATION STUFF ***
if a built-in option is detected on command line? In other words, is there a method to be called for knowing that a built-in option has been detected by the command line parser? Otherwise: is there a method for knowing that exit()
has been called on application, and hence the application is terminating? If so, it would suffice to enclose my *** SOME INITIALIZATION STUFF ***
in a if
condition argumenting on that method and it would be fine...
Thanks for the support.
>> EDIT <<
I've done some further investigations... the behavior seems not be dependent on my own *** SOME INITIALIZATION STUFF ***
... in fact even a "minimal, complete and verifiable example" as the following one sometimes terminates the execution with segmentation fault when the executable is triggered with --version
on the command line:
#include <QApplication>
#include <QCommandLineParser>
int main(int argc, char *argv[])
{
// Instantiate the application
QApplication app(argc, argv);
app.setApplicationVersion("1.0.0");
// Prepare for parsing the command line
QCommandLineParser cmdLineParser;
cmdLineParser.setApplicationDescription("Test application");
// Add support for command line standard options (help & version)
cmdLineParser.addHelpOption();
cmdLineParser.addVersionOption();
// Perform actual parsing of command line
cmdLineParser.process(app);
// Let the application run
return app.exec();
}
Just to clarify, I am using Qt 5.3.2 on an ARM device running Linux OS.