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

Optimizing Performance with Qt Creator

We don't use performance analysis tools every day, but we're glad that they're there when we need them. Commercial tools, such as the ones that come with Microsoft Visual Studio, or standalone tools, such as IBM's Rational Rose Purify, can set you back due to their complexity and beginner-unfriendly design. Fortunately, Qt Creator has most of what you need in terms of built-in support for working with open source tools to help you profile the runtime and the memory performance of your application.

In this chapter, we will see how to perform the runtime profiling of QML applications using the QML performance analyzer and learn how to read the reports it generates to identify performance issues. We will then turn our attention to memory performance analysis with Valgrind using Qt Creator, which is a free option that helps...

Technical requirements

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

Introducing QML performance analysis

Qt Quick applications are supposed to be fast, with smooth and fluid user interfaces. In many cases, this is easy to accomplish with QML; the contributors to QML and the Qt Quick runtime have put a great deal of effort into creating an environment that performs well in a wide variety of circumstances. Sometimes, however, try as you might, you will find that you just can't squeeze the performance out of your application that you'd like. Some mistakes are obvious, such as the following:

  • Doing a lot of compute-intensive tasks between state changes or actions that trigger drawing operations
  • Excessively complex view hierarchies with thousands of elements on display at once
  • Running on very limited hardware (often in combination with the first two problems)

Knuth famously said, "Premature optimization is the root of all evil,"...

Introducing QML Profiler

In the previous section, Introducing QML performance analysis, you were introduced to QML Profiler and its basic functionality. In this section, we will explore what other features QML Profiler can offer us to make our debugging process faster and more effective. We will also learn how to examine the data displayed in QML Profiler and what the data indicates, so that we can determine the cause of slowdowns or crashes happening to our application.

To demonstrate this, let's perform the following steps:

  1. Let's create a new Qt Quick project and change the code of main.qml to the following:
import QtQuick 2.12
import QtQuick.Window 2.12

Window {
id: window
visible: true
width: 640
height: 480
title: qsTr("Hello World")

Component.onCompleted: {
for (var i = 0; i < 200; i++)
{
var x = Math.floor...

Doing more with QML Profiler

Let's now learn how we can use QML profiler for something other than just normal GUI applications. In the following example, we will try and create a simple 3D renderer using Qt Quick and make use of QML Profiler to examine its performance.

First, let's create an empty Qt Quick project. Then, open up main.qml and add the following modules to it:

import QtQuick 2.12 as QtQuick2
import QtQuick.Scene3D 2.0
import Qt3D.Core 2.0
import Qt3D.Render 2.0
import Qt3D.Extras 2.0

We added the modules for 3D rendering that are required for later use. We also set an alias for the QtQuick module, which is QtQuick2, as we will be using it later in our code. After that, let's remove the default Window item and add Entity instead. We call this Entity instance sceneRoot, and it will act as the parent item for all objects in the 3D scene. We also add a Camera...

Implementing test integration

Unit testing is a very important stage during application development but is oftentimes ignored by developers, especially beginners. Unit testing ensures that the quality of your application is up to scratch and improves the user experience. One of the methods for unit testing is to integrate auto tests into your project. In this section, we will learn how we can implement different types of auto tests in Qt Creator.

The Qt Test framework consists of two parts—Qt Test and Qt Quick Test, which test C++, QML, and GUI features, whereas the other two frameworks only test C++ features. Pick the one that suits your project the most. We will look into each of them here.

Creating Qt and Qt Quick tests

...

Adding better support for test integration

Even though Qt and Qt Quick tests are both really good auto-testing frameworks, we can also integrate some other third-party unit testing frameworks to test for different issues. You can also compare the results from different frameworks to make sure there are no false positives and ensure that the quality of your product is at its best. Besides their own Qt Test framework, Qt Creator also integrates several other different auto test suites into the editor, such as Google C++ Testing Framework and Boost.Test, for automated unit testing.

First, we will learn how we can integrate Google Test into our project.

Creating Google tests

Before we start setting up Google Test, let's...

Summary

Qt Creator provides the QML analyzer, which lets you perform runtime analysis of your Qt applications. You can see a graph (in time) of how your application is running, as well as dive into the details about how your application spends its time drawing, binding to variables, and executing JavaScript.

Qt Creator also integrates well with Valgrind on Linux, letting you look for memory leaks in your application. Using Valgrind on Linux, you can see the blocks that were allocated but not freed, and, more importantly, see how big they were and where they were allocated in the code, giving you a head start in determining why they were not freed. Since Valgrind only works on Linux, we also discussed another memory leak detector called Visual Leak Detector, which works great on Windows.

Other than that, we have also learned how to implement automated unit testing using Qt Test...

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 $15.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