{"id":12161,"date":"2025-07-22T09:35:42","date_gmt":"2025-07-22T07:35:42","guid":{"rendered":"https:\/\/www.basyskom.de\/?p=12161"},"modified":"2025-07-22T09:36:13","modified_gmt":"2025-07-22T07:36:13","slug":"using-clang-format-in-qt-creator","status":"publish","type":"post","link":"https:\/\/www.basyskom.de\/en\/using-clang-format-in-qt-creator\/","title":{"rendered":"Using clang-format in Qt Creator"},"content":{"rendered":"<div data-elementor-type=\"wp-post\" data-elementor-id=\"12161\" class=\"elementor elementor-12161\" data-elementor-post-type=\"post\">\n\t\t\t\t<div class=\"elementor-element elementor-element-f816ecb e-flex e-con-boxed wpr-particle-no wpr-jarallax-no wpr-parallax-no wpr-sticky-section-no wpr-equal-height-no e-con e-parent\" data-id=\"f816ecb\" data-element_type=\"container\" data-e-type=\"container\">\n\t\t\t\t\t<div class=\"e-con-inner\">\n\t\t\t\t<div class=\"elementor-element elementor-element-565bab3 elementor-widget elementor-widget-heading\" data-id=\"565bab3\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t<h2 class=\"elementor-heading-title elementor-size-default\">The Benefits of Code Formatting<\/h2>\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-02a92fb elementor-widget elementor-widget-text-editor\" data-id=\"02a92fb\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>Enforcing a consistent code style for a code base greatly improves the readability and reduces the potential for merge conflicts when multiple developers work on the same project. While most companies have style guides, the code style is often not enforced automatically and developers must write their code to match the requirements.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-da92d0d elementor-widget elementor-widget-text-editor\" data-id=\"da92d0d\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>The <a href=\"https:\/\/clang.llvm.org\/docs\/ClangFormat.html\" target=\"_blank\" rel=\"noopener\">clang-format<\/a> toolset is our go-to option for automatic code formatting because of the easy integration into Qt Creator and its configurability.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-067508b elementor-widget elementor-widget-heading\" data-id=\"067508b\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t<h2 class=\"elementor-heading-title elementor-size-default\">Styles<\/h2>\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-6ee2c84 elementor-widget elementor-widget-text-editor\" data-id=\"6ee2c84\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>The clang-format tool supports several built-in style presets. If the style is set to <em>file<\/em>, custom styles are read from a <a href=\"https:\/\/clang.llvm.org\/docs\/ClangFormatStyleOptions.html\" target=\"_blank\" rel=\"noopener\">file<\/a> named <em>.clang-format<\/em> or <em>_clang-format<\/em> which must be located in the same directory or a parent directory of the source code location. The closest configuration file always overrides previous files, so subtrees of the project directory structure can use different code styles.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-a455516 elementor-widget elementor-widget-text-editor\" data-id=\"a455516\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>If the <em>file<\/em> style is used and no file is available, the default behavior of clang-format is to apply a fallback style. To prevent this, the fallback style option must be set to <em>none<\/em>.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-f82c004 elementor-widget elementor-widget-text-editor\" data-id=\"f82c004\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>For new projects without style requirements provided by the customer, we usually recommend the <a href=\"https:\/\/code.qt.io\/cgit\/qt\/qt5.git\/tree\/_clang-format\" target=\"_blank\" rel=\"noopener\">Qt style file<\/a> from the qt5 repository.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-3953751 elementor-widget elementor-widget-heading\" data-id=\"3953751\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t<h2 class=\"elementor-heading-title elementor-size-default\">Excluding Files and Directories<\/h2>\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-0f8f157 elementor-widget elementor-widget-text-editor\" data-id=\"0f8f157\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>To exclude files, directories or entire subtrees from automatic formatting, clang-format reads a file named <em><a href=\"https:\/\/clang.llvm.org\/docs\/ClangFormat.html#clang-format-ignore\" target=\"_blank\" rel=\"noopener\">.clang-format-ignore<\/a><\/em> which follows the same override logic as<em> .clang-format<\/em>.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-8b593e2 elementor-widget elementor-widget-text-editor\" data-id=\"8b593e2\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>This comes in handy if out project embeds source code from a git submodule without a <em>.clang-format<\/em> file where automatic formatting might mess up an entire file after one small change. By adding the directory to a <em>.clang-format-ignore<\/em> file residing in the repository under our control, we can prevent automatic formatting from being performed without making changes to the submodule repository and without placing untracked files in its directory.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-bc79a39 elementor-widget elementor-widget-text-editor\" data-id=\"bc79a39\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>Due to a bug in clang-format which was <a href=\"https:\/\/github.com\/llvm\/llvm-project\/pull\/113100\" target=\"_blank\" rel=\"noopener\">fixed in October 2024<\/a>, the way most auto formatting extensions for IDEs (including Qt Creator) call it ignored exclusions from the <em>.clang-format-ignore<\/em> file. The fix is already included in the current clang-format 20 releases.<\/p><p>If only versions up to clang-format 19 can be used during development, putting a <i>.clang-format<\/i> file containing the text\u00a0<i>DisableFormat: true<\/i> into the directory to exclude is a viable workaround.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-c427c6b elementor-widget elementor-widget-heading\" data-id=\"c427c6b\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t<h2 class=\"elementor-heading-title elementor-size-default\">Integration into Qt Creator<\/h2>\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-b037e94 elementor-widget elementor-widget-text-editor\" data-id=\"b037e94\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>The features of clang-format are available in Qt Creator by enabling the <a href=\"https:\/\/doc.qt.io\/qtcreator\/creator-preferences-beautifier.html\" target=\"_blank\" rel=\"noopener\">Beautifier<\/a> plugin. This adds a new entry named <i>Beautifier<\/i> to the preferences dialog.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-b220fcd elementor-widget elementor-widget-text-editor\" data-id=\"b220fcd\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>In the <i>General<\/i> tab, the Tool option must be set to <i>ClangFormat<\/i>. We also recommend enabling the <i>Automatic Formatting on File Save<\/i> checkbox to fully automate the formatting process.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-721b702 elementor-widget elementor-widget-image\" data-id=\"721b702\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"image.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<img fetchpriority=\"high\" decoding=\"async\" width=\"448\" height=\"206\" src=\"https:\/\/www.basyskom.de\/wp-content\/uploads\/2025\/06\/clangformat1.png\" class=\"attachment-large size-large wp-image-12166\" alt=\"basysKom, HMI Dienstleistung, Qt, Cloud, Azure\" srcset=\"https:\/\/www.basyskom.de\/wp-content\/uploads\/2025\/06\/clangformat1.png 448w, https:\/\/www.basyskom.de\/wp-content\/uploads\/2025\/06\/clangformat1-300x138.png 300w, https:\/\/www.basyskom.de\/wp-content\/uploads\/2025\/06\/clangformat1-18x8.png 18w\" sizes=\"(max-width: 448px) 100vw, 448px\">\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-c041c57 elementor-widget elementor-widget-text-editor\" data-id=\"c041c57\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>The <i>ClangFormat<\/i> tab contains a few more settings we need to adapt to our development setup and project requirements.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-008d807 elementor-widget elementor-widget-image\" data-id=\"008d807\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"image.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<img decoding=\"async\" width=\"800\" height=\"309\" src=\"https:\/\/www.basyskom.de\/wp-content\/uploads\/2025\/06\/clangformat2-1.png\" class=\"attachment-large size-large wp-image-12196\" alt=\"basysKom, HMI Dienstleistung, Qt, Cloud, Azure\" srcset=\"https:\/\/www.basyskom.de\/wp-content\/uploads\/2025\/06\/clangformat2-1.png 804w, https:\/\/www.basyskom.de\/wp-content\/uploads\/2025\/06\/clangformat2-1-300x116.png 300w, https:\/\/www.basyskom.de\/wp-content\/uploads\/2025\/06\/clangformat2-1-768x297.png 768w, https:\/\/www.basyskom.de\/wp-content\/uploads\/2025\/06\/clangformat2-1-18x7.png 18w, https:\/\/www.basyskom.de\/wp-content\/uploads\/2025\/06\/clangformat2-1-560x217.png 560w\" sizes=\"(max-width: 800px) 100vw, 800px\">\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-848585e elementor-widget elementor-widget-text-editor\" data-id=\"848585e\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>The <em>ClangFormat command<\/em>\u00a0option must be set to the path of the clang-format executable. Depending on the operating system, clang-format is available in the official repositories (most Linux distributions) or must be downloaded and installed (Windows).<\/p><p>The <i>predefined style<\/i> should be set to one of the preset styles or the <em>file<\/em> style. We recommend setting the fallback style to <em>none<\/em> in order to avoid unpleasant surprises when opening unrelated projects and files using the same Qt Creator configuration.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-e4460e5 elementor-widget elementor-widget-text-editor\" data-id=\"e4460e5\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>While the Beautifier plugin also provides a way to input the custom style options using the <i>Use customized style<\/i> radio button, we always recommend the file style. This allows checking the code style file(s) into our repository so they can be used by fellow developers without complex setup steps, no matter which development setup they use.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-8971a6b elementor-widget elementor-widget-heading\" data-id=\"8971a6b\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t<h2 class=\"elementor-heading-title elementor-size-default\">Conclusion<\/h2>\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-6697973 elementor-widget elementor-widget-text-editor\" data-id=\"6697973\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>With a few clicks, we have enabled automatic code formatting in Qt Creator and never have to think about braces placement, white spaces and breaking long lines ever again. If a file containing changes is saved, it will be formatted automatically according to the options defined in our <em>.clang-format<\/em> file.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-23e92ca elementor-widget elementor-widget-text-editor\" data-id=\"23e92ca\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>By adding the configuration files to the repository, the code style becomes part of the project and can be applied by most IDEs or by invoking clang-format from the command line.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-1bc99a2 elementor-widget elementor-widget-text-editor\" data-id=\"1bc99a2\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>The steps described above still require some cooperation from all developers. In order to really enforce the code style and to make sure that all developers contributing to the project apply clang-format, it could be a good idea to add a check to the CI pipeline which blocks merge requests if the committed code does not follow the rules of the .clang-format file.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<\/div>","protected":false},"excerpt":{"rendered":"<p>In this blog you will learn how to set up clang-format with Qt Creator for consistent, automatic code formatting, including custom style files and exclusion rules for subdirectories.<\/p>","protected":false},"author":4,"featured_media":4011,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"inline_featured_image":false,"footnotes":""},"categories":[1,2,8],"tags":[15],"class_list":["post-12161","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-allgemein","category-blog","category-qt","tag-qt"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.basyskom.de\/en\/wp-json\/wp\/v2\/posts\/12161","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.basyskom.de\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.basyskom.de\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.basyskom.de\/en\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.basyskom.de\/en\/wp-json\/wp\/v2\/comments?post=12161"}],"version-history":[{"count":52,"href":"https:\/\/www.basyskom.de\/en\/wp-json\/wp\/v2\/posts\/12161\/revisions"}],"predecessor-version":[{"id":12234,"href":"https:\/\/www.basyskom.de\/en\/wp-json\/wp\/v2\/posts\/12161\/revisions\/12234"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.basyskom.de\/en\/wp-json\/wp\/v2\/media\/4011"}],"wp:attachment":[{"href":"https:\/\/www.basyskom.de\/en\/wp-json\/wp\/v2\/media?parent=12161"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.basyskom.de\/en\/wp-json\/wp\/v2\/categories?post=12161"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.basyskom.de\/en\/wp-json\/wp\/v2\/tags?post=12161"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}