Reader small image

You're reading from  Application Development with Qt Creator - Third Edition

Product typeBook
Published inJan 2020
Reading LevelBeginner
Publisher
ISBN-139781789951752
Edition3rd Edition
Languages
Right arrow
Author (1)
Lee Zhi Eng
Lee Zhi Eng
author image
Lee Zhi Eng

Lee Zhi Eng is a self-taught programmer who worked as an artist and programmer at several game studios before becoming a part-time lecturer for 2 years at a university, teaching game development subjects related to Unity and Unreal Engine. He has not only taken part in various projects related to games, interactive apps, and virtual reality but has also participated in multiple projects that are more oriented toward software and system development. When he is not writing code, he enjoys traveling, photography, and exploring new technologies.
Read more about Lee Zhi Eng

Right arrow

Qt Tips and Tricks

In the previous chapters, we discussed what makes Qt great for software development: how to edit, compile, and debug applications; how to profile their execution and memory performance; how to localize them for different regions of the world; as well as how to make mobile applications that run on Android phones and tablets.

In this chapter, we will discuss a collection of tips and tricks that you should know about when using Qt Creator and Qt, which will have you writing software like a pro.

We will cover the following topics in this chapter:

  • Writing console applications with Qt Creator
  • Integration with version control systems
  • Configuring the coding style and coding format options
  • Applying a new theme to Qt Creator
  • Setting the Qt Quick window display options
  • Building projects from CMake and the command line
  • Running multiple debuggers simultaneously
  • Learning...

Technical requirements

The technical requirements for this chapter include Qt 5.12.3, MinGW 64-bit, Qt Creator 4.9.0, and Windows 10.

Writing console applications with Qt Creator

Remember Hello World in Chapter 1, Getting Started with Qt Creator? That was a console application – about as simple a console application as you can write. Let's recap the code: We created a new Qt console application and, in main.cpp, we wrote the following lines of code:

#include <QCoreApplication> 
#include <iostream> 
 
using namespace std; 
 
int main(int argc, char *argv[]) 
{ 
    QCoreApplication a(argc, argv); 
     
    cout << "Hello world!"; 
 
    return a.exec(); 
} 

Any valid C++ file is valid in a Qt application, including Standard Template Library (STL) code. This is especially handy if you need to write a small tool in C++ and haven't learned a lot about Qt yet – everything you know about C++ (and even C, if you prefer) is accessible to you in Qt Creator.

Although...

Integrating Qt Creator with version control systems

Nearly all large projects require some sort of version control to coordinate the changes made to the same files by different users and to ensure that changes to a source base occur harmoniously. Even a single developer can benefit from using version control because version control provides a record of what has changed in each file that the developer has edited and provides a valuable history of the project over time. Qt Creator supports the following version control systems:

  • Bazaar (supported in Qt Creator version 2.2 and beyond)
  • CVS
  • Git
  • Mercurial (supported in Qt Creator version 2.0 and beyond)
  • Perforce (supporting Perforce server version 2006.1 and later)
  • Subversion

Aside from these Qt Creator also supports some commercial version control hosting services, including the following:

  • GitHub
  • GitLab

The first thing you need...

Configuring the coding style and coding format options

Readable code is crucial, and Qt Creator's default coding style is one that most people find very readable. However, you might be on a project with different coding guidelines, or you might just find that you can't bear a particular facet of how the Qt Creator editor deals with code formatting; maybe it's the positioning of the brackets or how a switch statement gets formatted. Fortunately, Qt Creator is extremely configurable. Go to Tools | Options... | C++ and configure how Qt Creator will format your code, as shown in the following screenshot:

The basic dialog lets you pick popular formatting styles, such as Qt's default format or the format used by most GNU code. You can also click on Edit..., which brings up the code style editor, which you can see in the next screenshot:

You'll want to begin...

Applying new themes to Qt Creator

Other than configuring the code style, Qt Creator also allows us to change the color scheme and style of the entire program. This helps us change the user interface appearance! You can do so by going to Tools | Options...| Environment and you will see the Theme option under User Interface settings:

By default, Qt Creator is using the Flat theme, which is what we are familiar with all this time. Qt also provides us with some of the newer options, such as Dark, Design, Flat Dark, and Flat Light. Do try them out and see whether any of them fit your taste! Do note that you may need to restart Qt Creator in order for the change to happen.

There are some other third-party themes for Qt Creator that are worth mentioning here:

Setting the Qt Quick window display options

Qt Quick is great for building applications for non-traditional computing environments, such as set-top boxes or automotive computers. Often, when working with Qt Quick, you'll want an application that doesn't have all the usual windows (such as the close box) around the contents of the window in these settings, because you're trying to present a unified user interface based on your Qt Quick application, rather than the windowing toolkit on the host platform.

You can easily set windows options by editing the main.cpp file in your Qt Quick project. By default, it looks similar to the following code snippet:

#include <QGuiApplication>
#include <QtQuick/QQuickView>

int main(int argc, char *argv[])
{
QGuiApplication a(argc, argv);

QQuickView view;
view.setSource(QUrl("qrc:/qml/main.qml"));
view.show...

Building projects from CMake and the command line

Sometimes, you need to build a project from the command line. Maybe you're working on Linux and you're just more comfortable there, or you've got a remote session running on your desktop while you're in a meeting. Or, maybe you want to automate builds on a build server and need to know how Qt performs its compilation magic for your builds.

Building using qmake

The trick lies in qmake: Qt's meta-make system that manages the generation of make files for the compiler toolchain you have already installed. The qmake command takes the .pro files, which you first saw in Chapter 2, Building Applications with Qt Creator, and generates the make or nmake file...

Running multiple debuggers simultaneously

Oftentimes, our project is not merely a simple hello world application that runs only a single executable, but can be a collection of executables of different types that run on servers, client computers, or even mobile devices simultaneously. For example, your end user could be running the app you built from Qt, which runs on their mobile device. Then, you have a server application that is also built from Qt that processes information sent from the user's app. Finally, you provide your server admin with management software that you built with Qt that runs on their PC.

To build and maintain such a huge application, we need to make sure it is always easy and keeps things together. To ensure that we can easily edit, build, and debug all the different applications, we use something called subdir to group these projects together into one...

Learning more about Qt

In the first few chapters, I pointed you to the Help panel of Qt Creator as well as the editor's facility for the autocompletion of class members when editing code. Qt Creator's help view is really a subview of Qt Assistant, the full documentation for Qt. This should be installed by default if you install all of the Qt installations; the documentation is packaged as HTML files locally. Much of this documentation is also available on the web, but it's much faster to access it this way.

When we start Qt Assistant from the Qt SDK (either from the command line with assistant, or by finding it in the installed list of applications), we should see something similar to the following screenshot:

Qt Assistant is the definitive place to learn about Qt. In the column on the left-hand side, you can see a table of contents; the best place to start is...

Summary

Qt and Qt Creator provide a great environment for application development, irrespective of whether you're writing console, GUI, or Qt Quick applications. You can mix and match standard C++ code with Qt, making the most of your existing skills. When doing this, you can add in things such as version control and command-line builds to your tools, giving you the ability to work in large teams and perform unattended builds of large projects using Qt. Qt has great documentation too, both bundled with Qt Creator and on the web.

With what you've learned in this book and what's available, the sky's the limit for your application development goals!

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Application Development with Qt Creator - Third Edition
Published in: Jan 2020Publisher: ISBN-13: 9781789951752
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at £13.99/month. Cancel anytime

Author (1)

author image
Lee Zhi Eng

Lee Zhi Eng is a self-taught programmer who worked as an artist and programmer at several game studios before becoming a part-time lecturer for 2 years at a university, teaching game development subjects related to Unity and Unreal Engine. He has not only taken part in various projects related to games, interactive apps, and virtual reality but has also participated in multiple projects that are more oriented toward software and system development. When he is not writing code, he enjoys traveling, photography, and exploring new technologies.
Read more about Lee Zhi Eng