Currently I have a working Qt command line application. However, I need to refactor this working program such that my QCommandLineParser object gets configured in a class method rather than in main() itself. I have tried the obvious:
In ExecuteTask.h:
void setUp(QCommandLineParser parser);
In ExecuteTask.cpp:
void ExecuteTask::setUp(QCommandLineParser parser){
parser.setApplicationDescription("Learning console app in Qt");
parser.addHelpOption();
}
In main.cpp:
...
QCoreApplication app(argc, argv);
ExecuteTask cmnd_line_func;
QCommandLineParser parser;
cmnd_line_func.setUp(parser);
...
However, I get this error (attached in link): Compilation error
I have also tried declaring QCommandLineParser parser as a pointer in ExecuteTask.h but obviously this leads to problems when you have to run:
parser.process(app)
in main. I have tried actually also passing QCoreApplication app
to my setUp function to run parser.process(app)
in my setUp()
method but that brought up similar "...is private within this context".
Also tried another solution where declaring QCommandLineParser parser
as a pointer and using a getParser()
method to return the parser in main but this lead to similar "private" problems.
-- no idea where to go from here as I'm used to C++ and just passing argc and argv to methods but this with Qt is different.
So is there a way that QCommandLineParser can be passed to a method outside of main()? The docs didn't much help me and just about every tutorial I've come across has all configuration done in main() and this is not what I want to do at all.
Okay, after mucking around, I found the solution. in ExecuteTask.h:
void setUp(QCommandLineParser *parser);
in ExecuteTask.cpp:
void ExecuteTask::setUp(QCommandLineParser *parser){
parser->setApplicationDescription("Learning console app in Qt");
parser->addHelpOption();
}
in main.cpp:
ExecuteTask cmnd_line_func;
QCommandLineParser parser;
cmnd_line_func.setUp(&parser);
parser.process(app);