Disclaimer
Before reading this tutorial you, the reader, agree to the following. This tutorial and the code it contains are designed to be informational and educational tools only. They do not constitute investment advice. The author, Dave Topper, strongly recommends that you seek the advice of a financial services professional before making any type of investment. This model is provided as a rough approximation of future financial performance. The results presented are hypothetical and will likely not reflect the actual growth of your own investments. The author, Dave Topper, is not responsible for any human or mechanical errors or omissions. The author, Dave Topper, is not responsible for the consequences of any decisions or actions taken in reliance upon or as a direct or indirect result of the information provided by these tools.
With the basic setup now complete we can begin writing some code.
Open up the Sources section in the Project window (upper left hand corner of qtcreator).
Double click on the file main.cpp to load it into the editor so we can begin making some changes.
Your main.cpp should look like this.
#include "mainWindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
mainWindow w;
w.show();
return a.exec();
}
If you build and run the application as is all you will get a blank window which does nothing. That makes complete sense if you parse the above code. We’re going to modify things so that your application can be launched with a GUI interface or run as a command line app.
In order to do this we will create a function called createApplication which will return either a GUI app or a command line app depending on the command line arguments.
Add the following to main.cpp. I suggest adding it to the beginning of the file, after the #include statements.
// Return gui or command line version of the application
// =====================================================================
QCoreApplication* createApplication(int &argc, char *argv[])
{
for (int i = 1; i < argc; ++i)
if (!qstrcmp(argv[i], "-gui"))
{
// Return a QT application which includes the GUI
return new QApplication(argc, argv);
}
// Return a QT "core" application which does not contain a gui
return new QCoreApplication(argc, argv);
}
Then edit the main function as follows.
// Main, either runs as command line or launches a gui (from cmd line)
// ===================================================================
int main(int argc, char* argv[])
{
QScopedPointer<QCoreApplication> app(createApplication(argc, argv));
// Start GUI
if (qobject_cast<QApplication *>(app.data()))
{
mainWindow mW;
mW.show();
return app->exec();
}
// else Run non-GUI version...
else
{
std::cout << "Non gui version executed.\n";
}
}
You will also need to include <iostream> in order to access std::cout.
Your entire main.cpp should now look like this.
#include <QApplication>
#include <iostream>
#include "mainWindow.h"
// Return gui or command line version of the application
// ====================================================================
QCoreApplication* createApplication(int &argc, char *argv[])
{
for (int i = 1; i < argc; ++i)
if (!qstrcmp(argv[i], "-gui"))
{
// Return a QT application which includes the GUI
return new QApplication(argc, argv);
}
// Return a QT "core" application which does not contain a gui
return new QCoreApplication(argc, argv);
}
// Main, either runs as command line or launches a gui (from cmd line)
// ===================================================================
int main(int argc, char* argv[])
{
QScopedPointer<QCoreApplication> app(createApplication(argc, argv));
// Start GUI
if (qobject_cast<QApplication *>(app.data()))
{
mainWindow mW;
mW.show();
return app->exec();
}
// else Run non-GUI version...
else
{
std::cout << "Non gui version executed.\n";
}
}
Build and run the application again via the qtcreator IDE. You should get the following Application Output in the corresponding window.
Non gui version executed.
16:33:31: <path to your application>/algoTradingModel exited with code 0
Naturally, your timestamp and path will differ. But as you can see, the branch of code without the GUI was executed. In the remainder of this tutorial we will be running the application from the command line instead.
Now let’s look at how to run the application from the command line.
Select the Projects icon along the left hand border of the qtcreator IDE.
This will allow to you change various settings related to how your application is executed, debugged, etc.
Select Build beneath your kit definition in the Build & Run section in the left hand column of qtcreator as follows.
Uncheck the Shadow Build option. We won’t be creating different executables for different build configurations in this project. De-selecting Shadow Build will put the executable in the same directory as the source code, which will make things easier for us right now.
Build your application.
Navigate to the directory where your application is located and simply type the name of the application.
You should see the following output.
$ ./algoTradingModel
Non gui version executed.
$
Now run the application with the -gui flag.
A blank GUI window should load when the application is launched as follows.
$ ./algoTradingModel -gui
Simply exit the window and the application will close.
If you would prefer to run the application directly from within the qtcreator IDE, simply click the Projects Icon again and navigate to the Run section beneath your selected kit.
You can enter command line options in the Command Line Arguments section as shown below.
Please note, we will not be using the qtcreator IDE to run the application throughout the remainder of this tutorial.
There is one more piece we need to add in order to actually run the application from the command line. We need to create a “command line main” if you will. It will serve as the top level entry point for the rest of your application.
We will do that in the next section by creating a class of the same name.