Book Image

Application Development with Qt Creator - Third Edition

By : Lee Zhi Eng, Ray Rischpater
Book Image

Application Development with Qt Creator - Third Edition

By: Lee Zhi Eng, Ray Rischpater

Overview of this book

Qt is a powerful development framework that serves as a complete toolset for building cross-platform applications, helping you reduce development time and improve productivity. Completely revised and updated to cover C++17 and the latest developments in Qt 5.12, this comprehensive guide is the third edition of Application Development with Qt Creator. You'll start by designing a user interface using Qt Designer and learn how to instantiate custom messages, forms, and dialogues. You'll then understand Qt's support for multithreading, a key tool for making applications responsive, and the use of Qt's Model-View-Controller (MVC) to display data and content. As you advance, you'll learn to draw images on screen using Graphics View Framework and create custom widgets that interoperate with Qt Widgets. This Qt programming book takes you through Qt Creator's latest features, such as Qt Quick Controls 2, enhanced CMake support, a new graphical editor for SCXML, and a model editor. You'll even work with multimedia and sensors using Qt Quick, and finally develop applications for mobile, IoT, and embedded devices using Qt Creator. By the end of this Qt book, you'll be able to create your own cross-platform applications from scratch using Qt Creator and the C++ programming language.
Table of Contents (19 chapters)
1
Section 1: The Basics
7
Section 2: Advanced Features
12
Section 3: Practical Matters

Your first application – Hello World

In Qt Creator, select New File or Project from the File menu. Qt Creator will present you with the New File or Project wizard, which lets you choose the kind of project you want to create, give it a name, and so on. To create your first application, perform the following steps:

  1. Select New File or Project if you haven't done so already.
  2. Qt Creator presents you with a dialog that has a dizzying array of project choices. Choose Application, then Qt Console Application, and then click on Choose....
  3. Qt Creator asks you for a name and a path to the directory where you want to store the files for the project. For the name, enter HelloWorldConsole and choose a path that makes sense to you (or accept the default). Then, click on Next.
  4. Qt Creator asks you for the build system you want to use for your project. Just keep the default option, qmake, if you don't have any specific requirements for this. Then, click on Next:
  1. Qt Creator can support various kits and libraries against which you can build an application. Select the desktop Qt Kit, which should have been installed by default. If you're running Qt on Windows, please make sure that you select the desktop Qt Kit with MinGW, as it is installed by default. Make sure that you have installed Microsoft Visual Studio beforehand if you pick the desktop Qt MSVC Kit. Then, click on Next.
  2. In the next step, Qt Creator prompts you about the version control system for your project. Qt Creator can use your installed version control clients to perform change tracking for your project. For now, skip this and leave Add to version control set to None. Then, click on Finish.

Qt Creator creates your project and switches to the Edit view. In the source code editor for the main.cpp file, enter the highlighted code:

#include <QCoreApplication> 
#include <iostream> 
 
using namespace std; 

int main(int argc, char *argv[]) 
{ 
    QCoreApplication a(argc, argv); 
    cout << "Hello world!"; 
 
    return a.exec(); 
} 
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 emailed 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 (STL) 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:

  1. Click on the green run arrow underneath the Help view button on the left to run the application, as follows:
  1. Hit F5 to build and run your application in the debugger.
  2. Click on Start Debugging from the Debug menu, as follows:
  1. Click on the green run arrow with the bug over the arrow in order to debug the application on the left.
  2. Choose Run from the Build menu (or hit Ctrl + R).
If you only want to build the application, you can click on the hammer icon under the Run and Debug icons.

Once the application starts, you'll see the Hello world! message in a console view, as follows:

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 Chapter 2, Building Applications with Qt Creator) as it starts your application.