Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt Creator and other tools
  4. Clang Format and Qt Creator

Clang Format and Qt Creator

Scheduled Pinned Locked Moved Unsolved Qt Creator and other tools
6 Posts 3 Posters 4.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Bob64
    wrote on 13 Dec 2023, 22:23 last edited by Bob64
    #1

    I'm thoroughly confused with Qt Creator integration of Clang Format. There is a doc page about an experimental "Beautifier" plugin, which has a Clang Format mode apparently. But it looks like Qt Creator now has Clang Format built in, so do I just ignore the Beautifier? I don't see how to enable it in any case.

    So, let's assume I am using the built-in Clang Format. If I have a .clang-format file that I would like to use is there any way I can get Qt Creator to use it? I see this option:

    76f6be85-7585-4932-975b-534db7f70764-image.png

    which suggests that Qt Creator by default reads a .clang-format file. Moreover the tooltip help that pops up if I hover on that option talks about the project .clang-format file, which implies that I can add it to my project somehow. The natural place is at the root directory of the project as I want it to apply to all C++ files. My root .pro file is a simple subdirs and there does not appear to be an option to add a .clang-format here.

    Is there any documentation that explains all this anywhere?

    B 1 Reply Last reply 31 Jan 2024, 12:46
    0
    • B Bob64
      13 Dec 2023, 22:23

      I'm thoroughly confused with Qt Creator integration of Clang Format. There is a doc page about an experimental "Beautifier" plugin, which has a Clang Format mode apparently. But it looks like Qt Creator now has Clang Format built in, so do I just ignore the Beautifier? I don't see how to enable it in any case.

      So, let's assume I am using the built-in Clang Format. If I have a .clang-format file that I would like to use is there any way I can get Qt Creator to use it? I see this option:

      76f6be85-7585-4932-975b-534db7f70764-image.png

      which suggests that Qt Creator by default reads a .clang-format file. Moreover the tooltip help that pops up if I hover on that option talks about the project .clang-format file, which implies that I can add it to my project somehow. The natural place is at the root directory of the project as I want it to apply to all C++ files. My root .pro file is a simple subdirs and there does not appear to be an option to add a .clang-format here.

      Is there any documentation that explains all this anywhere?

      B Offline
      B Offline
      Bob64
      wrote on 31 Jan 2024, 12:46 last edited by
      #2

      Just bumping this as it was getting towards end of year/holiday season when I first posted it.

      Maybe my question doesn't make sense because I am just missing something obvious. Please tell me if this is the case.

      Any help with understanding how Clang Format is integrated in Qt Creator and how to configure it would be greatly appreciated.

      C 1 Reply Last reply 31 Jan 2024, 15:37
      0
      • B Bob64
        31 Jan 2024, 12:46

        Just bumping this as it was getting towards end of year/holiday season when I first posted it.

        Maybe my question doesn't make sense because I am just missing something obvious. Please tell me if this is the case.

        Any help with understanding how Clang Format is integrated in Qt Creator and how to configure it would be greatly appreciated.

        C Offline
        C Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on 31 Jan 2024, 15:37 last edited by
        #3

        So what's the actual problem? QtCreator reads a .clang-format file in the base source directory and uses it. Nothing more or less to do than creating this file and adding the desired options.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        B 1 Reply Last reply 31 Jan 2024, 16:58
        0
        • C Christian Ehrlicher
          31 Jan 2024, 15:37

          So what's the actual problem? QtCreator reads a .clang-format file in the base source directory and uses it. Nothing more or less to do than creating this file and adding the desired options.

          B Offline
          B Offline
          Bob64
          wrote on 31 Jan 2024, 16:58 last edited by
          #4

          @Christian-Ehrlicher I guess my question really is how do I add my own .clang-format file to a project and be sure it is being used. But the reason this seems not to be straightforward is that I don't have a good mental model of how the formatting is meant to work, I am confused by the preferences and settings dialogs, and I can't find any documentation that describes it.

          QtCreator reads a .clang-format file in the base source directory and uses it

          What is the definition of "base source directory"? Is that the same directory where my top-level pro file is? As I mentioned, that is just a subdirs file and I can't seem to add a .clang-format to it so how do I add it to my project?

          Let's look at the "Code Style" dialog:

          433897c3-e2f3-4484-947b-9d097dc40b56-image.png

          At the moment, "Use global settings" is ticked and the text below is disabled. If I untick it, the text "The current project has its own .clang-format file..." is enabled. This is enabled whether or not I put a .clang-format in my project though, so where is this file that it is referring to? How would I be able to tell whether it is using my file?

          Is that "Current settings" drop-down irrelevant unless I click "Override .clang-format file"? If so, why is it not disabled? If it is not irrelevant, what does it mean that the file can be overridden in the settings below? Does it mean that the .clang-format is still used but that the settings below are applied on top of the clang settings?

          Given that the "Use global settings" box is ticked, presumably this dialog is irrelevant. So let's go to "Global settings" (click on the link at the top of the dialog above).

          892d2192-9340-457b-a691-62a0b5625bbc-image.png

          So the "Code Style" tab here seems to suggest that it's using Clang Format. Is this with a "default" .clang-format file built in to Qt somewhere?

          What does the Current Settings selection mean here? Is it saying that the "QT [built-in]" is based on Clang Format? If I change it to "GNU [built-in]" none of the .clang-format values seem to change. So maybe the style is not purely defined by the .clang-format? Perhaps the tabs to the right of "ClangFormat" are relevant? Are they showing additional settings applied on top of the .clang-format settings?

          As I mentioned in my first post, the only mention of .clang-format that I can see in the docs is in the context of the "Beautifier" plugin, where it says that the plugin is not needed for Clang formatting as of Qt Creator 10.

          J 1 Reply Last reply 1 Feb 2024, 06:41
          0
          • B Bob64
            31 Jan 2024, 16:58

            @Christian-Ehrlicher I guess my question really is how do I add my own .clang-format file to a project and be sure it is being used. But the reason this seems not to be straightforward is that I don't have a good mental model of how the formatting is meant to work, I am confused by the preferences and settings dialogs, and I can't find any documentation that describes it.

            QtCreator reads a .clang-format file in the base source directory and uses it

            What is the definition of "base source directory"? Is that the same directory where my top-level pro file is? As I mentioned, that is just a subdirs file and I can't seem to add a .clang-format to it so how do I add it to my project?

            Let's look at the "Code Style" dialog:

            433897c3-e2f3-4484-947b-9d097dc40b56-image.png

            At the moment, "Use global settings" is ticked and the text below is disabled. If I untick it, the text "The current project has its own .clang-format file..." is enabled. This is enabled whether or not I put a .clang-format in my project though, so where is this file that it is referring to? How would I be able to tell whether it is using my file?

            Is that "Current settings" drop-down irrelevant unless I click "Override .clang-format file"? If so, why is it not disabled? If it is not irrelevant, what does it mean that the file can be overridden in the settings below? Does it mean that the .clang-format is still used but that the settings below are applied on top of the clang settings?

            Given that the "Use global settings" box is ticked, presumably this dialog is irrelevant. So let's go to "Global settings" (click on the link at the top of the dialog above).

            892d2192-9340-457b-a691-62a0b5625bbc-image.png

            So the "Code Style" tab here seems to suggest that it's using Clang Format. Is this with a "default" .clang-format file built in to Qt somewhere?

            What does the Current Settings selection mean here? Is it saying that the "QT [built-in]" is based on Clang Format? If I change it to "GNU [built-in]" none of the .clang-format values seem to change. So maybe the style is not purely defined by the .clang-format? Perhaps the tabs to the right of "ClangFormat" are relevant? Are they showing additional settings applied on top of the .clang-format settings?

            As I mentioned in my first post, the only mention of .clang-format that I can see in the docs is in the context of the "Beautifier" plugin, where it says that the plugin is not needed for Clang formatting as of Qt Creator 10.

            J Offline
            J Offline
            jsulm
            Lifetime Qt Champion
            wrote on 1 Feb 2024, 06:41 last edited by
            #5

            @Bob64 said in Clang Format and Qt Creator:

            so where is this file that it is referring to?

            @Christian-Ehrlicher literally wrote: "QtCreator reads a .clang-format file in the base source directory"

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            B 1 Reply Last reply 1 Feb 2024, 10:46
            0
            • J jsulm
              1 Feb 2024, 06:41

              @Bob64 said in Clang Format and Qt Creator:

              so where is this file that it is referring to?

              @Christian-Ehrlicher literally wrote: "QtCreator reads a .clang-format file in the base source directory"

              B Offline
              B Offline
              Bob64
              wrote on 1 Feb 2024, 10:46 last edited by Bob64 2 Jan 2024, 10:49
              #6

              @jsulm yes and I asked what was meant by that, because when I previously tried putting a .clang-format in my project root directory (where my top-level .pro file is), it didn't appear to have any effect.

              Having tried it again and set a ridiculous indent, I can now see that it does in fact appear to be using it. I think what was confusing me was that I expected to be able to see the effect previewed in the Settings dialog.

              At the very least, it would have been good to see some indication somewhere that Qt Creator has read my .clang-format file and is using it. (As I said, the dialog says "The current project has its own .clang-format file..." whether or not I have a .clang-format file in my "base source directory".)

              I maintain that the way this is presented in the Qt Creator UI is confusing. For example:

              • In Project Setting | Code Style, if I click "Use Global Settings" and go to the Global Settings, it shows me Clang Format settings that do not correspond to my file. And yet, if I edit my code, it applies the indentation I have applied in my local .clang-format. So, in what sense am I using Global Settings here?

              • In Project Settings | Code Style, if I don't click on the "Override .clang-format file" can I assume that the "Current settings" below is completely ignored? As I asked in my previous post, why then is this not disabled in that case?

              I guess if I could be sure that my .clang-format files is being read, and I could be sure that there are no interactions with any other Qt formatting mechanisms that are going to mess with my settings I would be happy.

              1 Reply Last reply
              2

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved