
Application Development with Qt Creator - Second Edition

In Qt Creator, select New File or Project… from the File menu. Qt Creator will present you with the New Project wizard, which lets you choose the kind of project you want to create, give it a name, and so forth. To create your first application, perform the following steps:
HelloWorldConsole
and choose a path that makes sense to you (or accept the default). Then, click on Next.Qt Creator creates your project and switches to the Edit view. In the source code editor for the main.cpp
file, enter the following code:
#include <QCoreApplication> #include <iostream> using namespace std; int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); cout << "Hello world!"; return a.exec(); }
Downloading the example code
You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
The QCoreApplication
task handles the entire system startup for an application, and every Qt Console app needs to create one and call its exec
method as part of your main
method. It sets up Qt's event handler and provides a bunch of porting helpers to determine things such as your application directory, library paths, and other details.
For a console application, that's all you need; you can freely mix and match Qt classes with the C++ standard library and Standard Template Library (although once you master Qt's foundation classes, many STL constructs might feel somewhat limiting).
Next, let's compile and run the application. There are several ways to do this. You can:
If you only want to build the application, you can click on the hammer icon below the Run and Debug icons.
When you choose one of these options, Qt Creator invokes the compiler and the linker to build your application. If you choose the Debug option, Qt Creator switches to the Debug view (which we will discuss in detail in the next chapter) as it starts your application.
Once the application starts, you'll see the Hello world! message in a console view.