Analyzing Code

To use a code analysis tool, select it in the Analyze Menu or in the pull-down menu of the (Start Debugging of Startup Project) button. In the Debug mode, switch between tools by selecting them in the menu on the debugger toolbar.

{Performance Analyzer in Debug mode}

Drag the views in the Debug mode to new positions on the screen. The size and position of views are saved for future sessions. Select View > Views > Reset to Default Layout to reset the views to their original sizes and positions.

Improving QML Performance

Qt Creator comes with the QML Profiler for inspecting binding evaluations and signal handling when running QML code. This is useful for identifying potential bottlenecks, especially in the evaluation of bindings.

Finding Issues in QML Code

Run static checks on the QML and JavaScript code in your project to find common problems, similarly to using JSLint.

Finding Issues in C++ Code with Clang Tools

Qt Creator comes with the following Clang tools for finding problems in C, C++, and Objective-C source code by using static analysis:

  • Clang-Tidy, which has diagnostics and fixes for typical programming errors, such as style violations or interface misuse.
  • Clazy, which helps Clang understand Qt semantics. It displays Qt-related compiler warnings, ranging from unnecessary memory allocation to misuse of API and has refactoring actions for fixing some of the issues.

Finding Issues in C++ Code with Cppcheck

Install the Cppcheck static analysis tool to detect undefined behavior and dangerous coding constructs in C++ code. Cppcheck analyzes the source code without actually running the application.

Checking Code Coverage

Install the Coco code coverage toolchain for Tcl, QML, C# and C/C++ programs on macOS, Linux, or Windows to analyze the way an application runs as part of a test suite, for example. Use the results to make the tests more efficient and complete.

You can:

  • Find untested code sections.
  • Find redundant tests which can then be eliminated. Coco can identify portions of the source code that are covered by a test. It can detect whether a new test covers lines in the source code that the existing tests do not cover.
  • Find dead code by displaying code that is never executed.
  • Calculate the optimum test execution order so as to maximize test coverage for each run. This is particularly useful for manual testing.
  • Analyze two separate versions of an application and compare the differences. This makes it possible to see which tests are affected by source code modifications and also to get some measure of the test coverage of a patch or hot fix.
  • Measure the execution time of applications and tests.

Preventing Code Erosion

Install Axivion Suite to protect software from erosion. With static code analysis, architecture analysis, and code-smells-detection, you can:

  • Check the source code for potential runtime errors.
  • Use metrics to generate quantitative information about the internal quality of the source code.
  • Run style checks to achieve compliance with coding guidelines.
  • Detect both duplicates and similar pieces of code in the source code.
  • Recognize cyclical dependencies at different levels.
  • Detect unreachable code.

Profiling with Valgrind Tools

Install Memcheck and Callgrind from Valgrind's Tool Suite to detect memory leaks and profile function execution.

You can run the Valgrind tools either locally on the development host or remotely on another host. You can use them to analyze both applications for which you set up a project in Qt Creator and applications for which you do not have a project.

Valgrind tools are supported locally only on Linux and macOS. However, according to Valgrind.org, support on macOS 10.8 and 10.9 is experimental and mostly broken. You can run the tools on a remote Linux machine or device from any development host.

To run the Valgrind tools to analyze an application for which you have a project, open the project in Qt Creator and select the kit to run the project. The kit specifies whether the Valgrind tools are run locally or remotely.

For more information about analyzing applications for which you do not have a project, see Run Valgrind tools on external applications.

To set preferences for the Valgrind tools, select Preferences > Analyzer. You can override the general settings for each project in the Run Settings for the project.

Detecting Memory Leaks with Heob

On Windows, install the Heob heap observer to detect buffer overruns and memory leaks.

Analyzing CPU Usage

On Linux, use Perf to analyze the CPU and memory usage of an application on Linux desktop and embedded devices.

Performance Analyzer uses the Perf tool bundled with the Linux kernel to take periodic snapshots of the call chain of an application and visualizes them in a timeline view or as a flame graph.

Visualizing Chrome Trace Events

Use the Chrome Trace Format Visualizer to view Chrome trace events. This is especially useful when viewing large trace files that are difficult to visualize using the built-in trace-viewer (chrome://tracing).

See also Activate kits for a project, How To: Analyze, and Analyzers.

© 2024 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.