Using Qt Creator's QML profiler

For this purpose, we will use a small program inspired by one of the technical videos from Qt's site about integrating Python and QML. It uses QML for UI definition, but defines a C++ model; thus, it is a mixed application. After starting, it will fetch data from the web to answer a question we posed, as can be seen in the following figure:

The source code of the program is contained in this book resources (https://github.com/PacktPublishing/Hands-On-High-performance-with-QT/tree/master/Chapter%202). We will use this application in the following examples. First, we will have a look at the performance profile of the QML part.

The QML Profiler is integrated with Qt Creator, so we only need to open the example project and select Analyze | QML Profiler to show the profiling view and then select the Start button to start the application with profiling enabled. QML debug support has to be enabled for the application for doing that, but in debug mode Qt Creator enables it by default. We can also attach the QML Profiler to a running application if we wish.

I started the QML Profiler for the example project previously mentioned, waited till it fetched the HTTP data with country names, and then scrolled the list up and down a little. Then, I stopped the profiling session and the following screen appeared:

We can see that, on the upper panel, the percentage of samples for each code line is shown, indicating that most of the work was done in the Text delegate, namely more than 75%. This is understandable, as I scrolled and flicked through the country names list. You'll also notice that not all the HTTP work appear in the profile, as QML Profiler does not look into C++ code.

On the lower end of the window, you'll notice a small Timeline panel, where the entire course of our performance data is shown in a minified manner, and which can be scrolled along the time coordinate using an opaque scene rectangle.

In the middle of the profiling screen, there is a list of different performance indicators. We can see some Scene Graph activity at the beginning, amounting to initialization. If we scroll the graphs using the opaque scene rectangle in the Timeline panes, we arrive at the following visualization:

We see that there was much activity in the Binding and JavaScript categories, and when we click on some of the bars in the Binding sections, the corresponding QML line will be selected, in our case, the text:modelData line, where the binding of C++ model data takes place. When we scroll up back to the Scene Graph category, we can see that here, too, a lot of fork was done when we were scrolling:

When we click on the smallest purple bar, down in the Scene Graph area, a popup with a description of that activity (here, Glyph Upload) and performance data shows up. If you want to learn more about data collection and analysis with the QML Profiler, please refer to the excellent Qt documentation.

We have seen that, when using the integrated QML Profiler, we can look rather deeply into the inner workings of QML code, and can obtain both a lot of detailed information and a good general overview of the behavior of our code. However, one thing immediately catches the eye—the absence of performance data between initialization and the scrolling of the list, that is, the HTTP communication. This is because QML Profiler is blind to C++ code, and that is why we need one additional tool: a general C++ CPU profiler.