Clang Format and Qt Creator
-
wrote on 13 Dec 2023, 22:23 last edited by Bob64
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: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 simplesubdirs
and there does not appear to be an option to add a.clang-format
here.Is there any documentation that explains all this anywhere?
-
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: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 simplesubdirs
and there does not appear to be an option to add a.clang-format
here.Is there any documentation that explains all this anywhere?
wrote on 31 Jan 2024, 12:46 last edited byJust 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.
-
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.
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.
-
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.
wrote on 31 Jan 2024, 16:58 last edited by@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:
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).
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. -
@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:
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).
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.@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"
-
@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"
wrote on 1 Feb 2024, 10:46 last edited by Bob64 2 Jan 2024, 10:49@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. -