Game Programming using Qt 5 Beginner's Guide
上QQ阅读APP看书,第一时间看更新

What just happened?

Creator created a new subdirectory in the directory that you previously chose for the location of the project. This new directory (the project directory) now contains a number of files. You can use the Projects pane of Qt Creator to list and open these files (refer to Chapter 2Installation, for an explanation of Qt Creator's basic controls). Let's go through these files.

The main.cpp file contains an implementation of the main() function, the entry point of the application, as the following code shows:

#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}

The main() function creates an instance of the QApplication class and feeds it with variables containing the command-line arguments. Then, it instantiates our MainWindow class, calls its show method, and finally, returns a value returned by the exec method of the application object.

QApplication is a singleton class that manages the whole application. In particular, it is responsible for processing events that come from within the application or from external sources. For events to be processed, an event loop needs to be running. The loop waits for incoming events and dispatches them to proper routines. Most things in Qt are done through events: input handling, redrawing, receiving data over the network, triggering timers, and so on. This is the reason we say that Qt is an event-oriented framework. Without an active event loop, the event handling would not function properly. The exec() call in QApplication (or, to be more specific, in its base class—QCoreApplication) is responsible for entering the main event loop of the application. The function does not return until your application requests the event loop to be terminated. When that eventually happens, the main function returns and your application ends.

The mainwindow.h and the mainwindow.cpp files implement the MainWindow class. For now, there is almost no code in it. The class is derived from QMainWindow (which, in turn, is derived from QWidget), so it inherits a lot of methods and behavior from its base class. It also contains a Ui::MainWindow *ui field, which is initialized in the constructor and deleted in the destructor. The constructor also calls the ui->setupUi(this); function.

Ui::MainWindow is an automatically generated class, so there is no declaration of it in the source code. It will be created in the build directory when the project is built. The purpose of this class is to set up our widget and fill it with content based on changes in the form editor. The automatically generated class is not a QWidget. In fact, it contains only two methods: setupUi, which performs the initial setup, and retranslateUi, which updates visible text when the UI language is changed. All widgets and other objects added in the form editor are available as public fields of the Ui::MainWindow class, so we can access them from within the MainWindow method as ui->objectName.

mainwindow.ui is a form file that can be edited in the visual form editor. If you open it in Qt Creator by double-clicking on it in the Projects pane, Qt Creator will switch to the Design mode. If you switch back to the Edit mode, you will see that this file is actually an XML file containing the hierarchy and properties of all objects edited in Design mode. During the building of the project, a special tool called the User Interface Compiler converts this XML file to the implementation of the Ui::MainWindow class used in the MainWindow class.

Note that you don't need to edit the XML file by hand or edit any code in the  Ui::MainWindow class. Making changes in the visual editor is enough to apply them to your MainWindow class and make the form's objects available to it.

The final file that was generated is called tictactoe.pro and is the project configuration file. It contains all the information that is required to build your project using the tools that Qt provides. Let's analyze this file (less important directives are omitted):

QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = tictactoe
TEMPLATE = app
SOURCES += main.cpp mainwindow.cpp
HEADERS += mainwindow.h
FORMS   += mainwindow.ui

The first two lines enable Qt's core, gui, and widgets modules. The TEMPLATE variable is used to specify that your project file describes an application (as opposed to, for example, a library). The TARGET variable contains the name of the produced executable (tictactoe). The last three lines list all files that should be used to build the project.

In fact, qmake enables Qt Core and Qt GUI modules by default, even if you don't specify them explicitly in the project file. You can opt out of using a default module if you want. For example, you can disable Qt GUI by adding  QT -= gui to the project file.

Before we proceed, let's tell the build system that we want to use C++11 features (such as lambda expressions, scoped enumerations, and range-based for loops) in our project by adding the following line to tictactoe.pro:

CONFIG += c++11

If we do this, the C++ compiler will receive a flag indicating that C++11 support should be enabled. This may not be needed if your compiler has C++11 support enabled by default. If you wish to use C++14 instead, use CONFIG += c++14.

What we have now is a complete Qt Widgets project. To build and run it, simply choose the Run entry from the Build drop-down menu or click on the green triangle icon on the left-hand side of the Qt Creator window. After a while, you should see a window pop up. Since we didn't add anything to the window, it is blank: