Book Image

Hands-On GUI Programming with C++ and Qt5

By : Lee Zhi Eng
Book Image

Hands-On GUI Programming with C++ and Qt5

By: Lee Zhi Eng

Overview of this book

Qt 5, the latest version of Qt, enables you to develop applications with complex user interfaces for multiple targets. It provides you with faster and smarter ways to create modern UIs and applications for multiple platforms. This book will teach you to design and build graphical user interfaces that are functional, appealing, and user-friendly. In the initial part of the book, you will learn what Qt 5 is and what you can do with it. You will explore the Qt Designer, discover the different types of widgets generally used in Qt 5, and then connect your application to the database to perform dynamic operations. Next, you will be introduced to Qt 5 chart which allows you to easily render different types of graphs and charts and incorporate List View Widgets in your application. You will also work with various Qt modules, like QtLocation, QtWebEngine, and the networking module through the course of the book. Finally, we will focus on cross-platform development with QT 5 that enables you to code once and run it everywhere, including mobile platforms. By the end of this book, you will have successfully learned about high-end GUI applications and will be capable of building many more powerful, cross-platform applications.
Table of Contents (18 chapters)

What is Qt?

Currently, the latest version of Qt (as this book is being written) is version 5.10. This version incorporated a lot of new features as well as thousands of bug fixes, which makes Qt a really powerful and stable development kit for software developers and system engineers alike. Qt has a huge package of SDK (software development kit) that contains a wide range of tools and libraries for helping developers get their job done without worrying too much about technical issues related to a specific platform. Qt handles all the messy integration and compatibility issues for you behind the curtain so you don't have to deal with them. This will not only improve efficiency but also reduces development costs, especially when you're trying to develop cross-platform applications that cater to a wider range of users.

There are two types of license for Qt:

  • The first type is the Open Source License, which is free of charge, but only if your project/product fits their terms and conditions. For example, if you made any changes to the Qt's source code, it is an obligation for you to submit back those changes to Qt developers. Failure to do so could result in serious legal issues, and therefore, you might want to pick the second option instead.
  • The second type of license is the Commercial License, which gives you full rights to proprietary Qt source code modifications and keeps your application private. But of course, these privileges come with a set of fees.

If you're just starting to learn Qt, don't get pushed back by these terms, as you're certainly not going to modify the source code of Qt libraries or recompile it from source anyway, at least not now.

For more information regarding Qt's licensing, please visit https://www.qt.io/licensing-comparison.

Why use Qt?

It's not hard to see why Qt stands a chance of winning against all other existing SDKs out there in the market; first of all, cross-platform compatibility. You can hardly find any other development kits that support so many platforms without writing different sets of code for each platform. By eliminating these extra steps, programmers can just focus on developing their applications without the need to worry about the implementation of each and every platform-specific feature. Furthermore, your code will look clean without all the #ifdef macros and having to load different dependencies for different platforms.

Qt generally uses C++, which is a compiled language that generates small and efficient code. It is also well documented and follows a very consistent set of naming conventions, which reduces the learning curve for the developer.

Do be aware that Qt does include a small amount of features that only work on specific platforms. However, these are minimal and often for special use cases, such as Qt Sensors, which only work on mobile platforms; Qt Web Engine, which only works on desktops; Qt NFC, only for Android and Linux; and so on. Those are some very specific functionalities that only exist on certain platforms that support them. Other than that, common features are usually supported on all platforms.

Discovering tools in Qt

Qt comes with a set of tools that make programmers' lives easier. One of the tools is Qt Creator (seen in the following screenshot), which is an IDE (integrated development environment) that consists of a code editor and a GUI (graphical user interface) designer that works hand-in-hand with other Qt tools, such as the compiler, debugger, and so on. The most attractive tool among all is, of course, the GUI designer, which comes with two different types of editors: one for widget-based applications, called Qt Designer, and another for Qt Quick Application, called Qt Quick Designer. Both tools can be accessed directly in Qt Creator when you open up a relevant file format. Qt Creator also includes a built-in documentation viewer called Qt Assistant. It is really handy since you can look for the explanation about a certain Qt class or function by simply hovering the mouse cursor over the class name in your source code, and pressing the F1 key. Qt Assistant will then be opened and show you the documentation related to the Qt class or function:

Qt Designer

Qt Designer is normally used by developers to design GUIs for desktop applications, while Qt Quick Designer is usually used for mobile and embedded platforms. With that being said, both formats run just fine on both desktop and mobile formats, the only difference is the look and feel, and the types of languages used.

The GUI file saved by Qt Designer carries the .ui extension, which is saved in XML format. The file stores the attributes of each and every widget placed by the GUI designer, such as position, size, margin, tooltip, layout direction, and so on. It also saves the signal-and-slot event names within itself for easily connecting with the code in the later stages. This format does not support coding and only works for Qt C++ projects, namely widget-based application projects.

Qt Quick Designer

On the other hand, Qt Quick Designer saves GUI files in both .ui.qml and .qml formats. Qt Quick is a very different type of GUI system in terms of technological concept and development approach, which we will cover in Chapter 14, Qt Quick and QML. Instead of XML format, Qt Quick Designer saves its data in a declarative language similar to JavaScript called QML. QML not only allows the designer to customize their GUI in a CSS-like (Cascading Style Sheets) fashion, it also allows the programmer to write functional JavaScript within the QML file. As we mentioned earlier, .ui.qml is the file format used for visual decoration only while .qml contains application logic.

If you're doing a simple program using Qt Quick, you don't have to touch any C++ coding at all. That's especially welcoming for web developers because they can immediately pick up Qt Quick and develop their own application without a steep learning curve; everything is just so familiar to them. For much more complex software, you can even link C++ functions from QML, and vice versa. Again, if you're interested in learning more about Qt Quick and QML, please head over to Chapter 14, QtQuick and QML.

Since Qt Creator is also written in Qt libraries itself, it is also totally cross-platform. Hence, you can use the same set of tools across different development environments and develop a unified workflow for your team, resulting in better efficiency and cost-effectiveness.

Other than that, Qt comes with many different modules and plugins, which cover a wide range of functionality you need for your projects. There is often no need for you to look for other external libraries or dependencies and try and implement them yourself. The abstraction layer of Qt makes the backend implementation invisible to the users and results in a unified coding style and syntax. If you try to put together a bunch of external dependencies yourself, what you'll find is each library has its own distinctive coding style. It's quite a mess when mixing up all the different coding styles in the same project, unless you make your own abstraction layer, which is a very time-consuming task. Since Qt already includes most, if not all the modules that you need to create feature-rich applications, there is no need for you to implement your own.

For more information regarding the modules that come with Qt, please visit: http://doc.qt.io/qt-5/qtmodules.html.

That being said, there are also many third libraries out there that extend Qt for features that Qt itself does not support, such as libraries that focus on game development or any other features that are designed for the specific user group.