Qt Creator C++ folding region
-
wrote on 3 Mar 2020, 09:01 last edited by JonB 3 Mar 2020, 09:02
I am trying out C++ & Qt Creator. It looks like this is a forlorn hope, but we are now in 2020, is there still no equivalent of
#region
from C# or Python, or#pragma region
from Visual Studio, etc., to define one's own folding regions in C++ code from Qt Creator? Personally I use these all the time. I see e.g. https://stackoverflow.com/questions/11295361/code-folding-pragma-region-in-qt-creator from 2012 or https://bugreports.qt.io/browse/QTCREATORBUG-5170 from 2011, but the suggested "workarounds" don't handle nearly enough cases. If it hasn't been done in a decade it doesn't look like it will get done now, does it...? -
I am trying out C++ & Qt Creator. It looks like this is a forlorn hope, but we are now in 2020, is there still no equivalent of
#region
from C# or Python, or#pragma region
from Visual Studio, etc., to define one's own folding regions in C++ code from Qt Creator? Personally I use these all the time. I see e.g. https://stackoverflow.com/questions/11295361/code-folding-pragma-region-in-qt-creator from 2012 or https://bugreports.qt.io/browse/QTCREATORBUG-5170 from 2011, but the suggested "workarounds" don't handle nearly enough cases. If it hasn't been done in a decade it doesn't look like it will get done now, does it...?@JonB said in Qt Creator C++ folding region:
It looks like this is a forlorn hope
As is usual and proper with hope.
is there still no equivalent of
#region
from C# or Python, or#pragma region
from Visual Studio, etc., to define one's own folding regions in C++ code from Qt Creator?No, I don't believe so.
Personally I use these all the time.
That's on you, I for one don't long for such a feature. There's no (IDE) substitute for well structured code is my usual rationalization.
If it hasn't been done in a decade it doesn't look like it will get done now, does it...?
Unless you're up to the task of implementing it, I wouldn't hold my breath.
-
@JonB said in Qt Creator C++ folding region:
It looks like this is a forlorn hope
As is usual and proper with hope.
is there still no equivalent of
#region
from C# or Python, or#pragma region
from Visual Studio, etc., to define one's own folding regions in C++ code from Qt Creator?No, I don't believe so.
Personally I use these all the time.
That's on you, I for one don't long for such a feature. There's no (IDE) substitute for well structured code is my usual rationalization.
If it hasn't been done in a decade it doesn't look like it will get done now, does it...?
Unless you're up to the task of implementing it, I wouldn't hold my breath.
wrote on 3 Mar 2020, 10:40 last edited by@kshegunov
Thank you for replying. At least I know where I stand, and that I not missing some other workaround. I will accept your response and mark this as (sadly) closed.There's no (IDE) substitute for well structured code is my usual rationalization.
My code is perfectly well structured & laid out, thank you :) You may be happy scrolling through lists of
#include
s or through, say, an adjacent list of half a dozen related functions within a class which are not to be regularly examined, but I am not. The fact that folding regions is incorporated into, say, Java, C#, Python and even Visual Studio C++ should indicate that I am not alone and that this is a common feature of modern IDEs! ;-) -
@kshegunov
Thank you for replying. At least I know where I stand, and that I not missing some other workaround. I will accept your response and mark this as (sadly) closed.There's no (IDE) substitute for well structured code is my usual rationalization.
My code is perfectly well structured & laid out, thank you :) You may be happy scrolling through lists of
#include
s or through, say, an adjacent list of half a dozen related functions within a class which are not to be regularly examined, but I am not. The fact that folding regions is incorporated into, say, Java, C#, Python and even Visual Studio C++ should indicate that I am not alone and that this is a common feature of modern IDEs! ;-)@JonB if your goal is to use it as some kind of documentation, have you considered using https://doc.qt.io/qt-5/qdoc-index.html ? its the same thing that is used to (auto) generate Qt-Documentation
🤗
-
@JonB if your goal is to use it as some kind of documentation, have you considered using https://doc.qt.io/qt-5/qdoc-index.html ? its the same thing that is used to (auto) generate Qt-Documentation
🤗
wrote on 3 Mar 2020, 10:48 last edited by JonB 3 Mar 2020, 10:50@J-Hilk
Thanks, but no, being a Real Programmer I am not, of course, looking to document anything I write ;-) No, what I am looking for is the ability to fold down related, adjacent areas of code so that I don't have to scroll through it on my screen. Purely as an example, if I have a few adjacent functions dealing with serialization/deserialization in a class, I like to fold these away under#region Serialization
(which will collapse to one line, showing theSerialization
word so I know what is there), and similarly with some other grouped functionality. -
@J-Hilk
Thanks, but no, being a Real Programmer I am not, of course, looking to document anything I write ;-) No, what I am looking for is the ability to fold down related, adjacent areas of code so that I don't have to scroll through it on my screen. Purely as an example, if I have a few adjacent functions dealing with serialization/deserialization in a class, I like to fold these away under#region Serialization
(which will collapse to one line, showing theSerialization
word so I know what is there), and similarly with some other grouped functionality.You could split that off into a different translation unit, or even an included cpp, you know. :)
It's a poor man's folding, I admit, but it sorta does what you want. -
You could split that off into a different translation unit, or even an included cpp, you know. :)
It's a poor man's folding, I admit, but it sorta does what you want.wrote on 3 Mar 2020, 11:00 last edited by@kshegunov
Yes I know I might be able to do that, but so could people in Java/Python/C#/VS C++, but they don't have to and there's a reason why... :) -
@J-Hilk
Thanks, but no, being a Real Programmer I am not, of course, looking to document anything I write ;-) No, what I am looking for is the ability to fold down related, adjacent areas of code so that I don't have to scroll through it on my screen. Purely as an example, if I have a few adjacent functions dealing with serialization/deserialization in a class, I like to fold these away under#region Serialization
(which will collapse to one line, showing theSerialization
word so I know what is there), and similarly with some other grouped functionality.wrote on 3 Mar 2020, 19:07 last edited by@JonB You wil be happy to find out that you can do this!
Goto tools>options>display>display>see image.
-
@JonB You wil be happy to find out that you can do this!
Goto tools>options>display>display>see image.
wrote on 3 Mar 2020, 19:52 last edited by@JoeJoe_000
I would be happy if that did it (don't have Qt Creator to hand atm), but surely it does not? Doesn't that just determine whether folding markers are visible or not, nothing to do with my question about wanting#region
-type folding areas? -
@JoeJoe_000
I would be happy if that did it (don't have Qt Creator to hand atm), but surely it does not? Doesn't that just determine whether folding markers are visible or not, nothing to do with my question about wanting#region
-type folding areas?@JonB said in Qt Creator C++ folding region:
Doesn't that just determine whether folding markers are visible or not
Yes, it does.
-
wrote on 4 Mar 2020, 22:14 last edited by
If you encapsulate in { }.
-
If you encapsulate in { }.
wrote on 5 Mar 2020, 08:20 last edited by JonB 3 May 2020, 08:21@Justin-Pattison-Schmidt
You can't encapsulate multiple function definitions inside{}
, nor lists of#include
statements.... -
@Justin-Pattison-Schmidt
You can't encapsulate multiple function definitions inside{}
, nor lists of#include
statements....@JonB you could encapsulate it in
#ifdef
and#endif
but that won't make magically create folding markers either 🤷♂️When IMHO it actually should!
-
@JonB you could encapsulate it in
#ifdef
and#endif
but that won't make magically create folding markers either 🤷♂️When IMHO it actually should!
wrote on 5 Mar 2020, 08:39 last edited by JonB 3 May 2020, 08:39@J-Hilk
I would be prepared to do that (well, probably#ifndef
rather, so that I don't have to do anything for the code to still be active!). But if it doesn't give me my folding it doesn't help, does it? :)As you say, I checked an it does not fold at
#if
-ers. Lazy Creator! :( Even worse, I see that if the expression is false (e.g.#if 0
) you lose the ability to fold the individual methods within it (presumably because it no longer has to examine/parse content). So if you have a large list of functions which are all#if
-ed out for whatever reason, you then have to have all the code expanded and scroll through it all the time. Grrr! -
wrote on 9 Feb 2021, 14:37 last edited by
I know this is old, but here is what works for me in QT Creator 4.13.3.
- Make sure 'Display Folding markers' is enabled
- 'Tools' -> 'Options' -> 'Text Editor' -> 'Display' tab
- In the '.cpp' or '.h' file you want to add the region to:
- Add '#pragma region RegionNameHere{'
- Note the '{' at the end
- Add '#pragma endregion }'
- Note the '}' at the end
- Add '#pragma region RegionNameHere{'
Some sample code:
#pragma region TIMER:Filter Change Delay { void CLog_EntryList::init_Timer_FilterChangeDelay() { m_timerFilterChangeDelay.setInterval(5000); m_timerFilterChangeDelay.setSingleShot(true); connect(&m_timerFilterChangeDelay, &QTimer::timeout, this, &CLog_EntryList::slot_Timer_FilterChangeDelay_Timedout); } void CLog_EntryList::slot_Timer_FilterChangeDelay_Start() { if(m_timerFilterChangeDelay.isActive()) return; m_timerFilterChangeDelay.setInterval(5000); m_timerFilterChangeDelay.setSingleShot(true); m_timerFilterChangeDelay.start(); } void CLog_EntryList::slot_Timer_FilterChangeDelay_Timedout() { } #pragma endregion}
And some proof in the pudding:
Not Folded
Folded
Enjoy ;)
- Make sure 'Display Folding markers' is enabled
-
I know this is old, but here is what works for me in QT Creator 4.13.3.
- Make sure 'Display Folding markers' is enabled
- 'Tools' -> 'Options' -> 'Text Editor' -> 'Display' tab
- In the '.cpp' or '.h' file you want to add the region to:
- Add '#pragma region RegionNameHere{'
- Note the '{' at the end
- Add '#pragma endregion }'
- Note the '}' at the end
- Add '#pragma region RegionNameHere{'
Some sample code:
#pragma region TIMER:Filter Change Delay { void CLog_EntryList::init_Timer_FilterChangeDelay() { m_timerFilterChangeDelay.setInterval(5000); m_timerFilterChangeDelay.setSingleShot(true); connect(&m_timerFilterChangeDelay, &QTimer::timeout, this, &CLog_EntryList::slot_Timer_FilterChangeDelay_Timedout); } void CLog_EntryList::slot_Timer_FilterChangeDelay_Start() { if(m_timerFilterChangeDelay.isActive()) return; m_timerFilterChangeDelay.setInterval(5000); m_timerFilterChangeDelay.setSingleShot(true); m_timerFilterChangeDelay.start(); } void CLog_EntryList::slot_Timer_FilterChangeDelay_Timedout() { } #pragma endregion}
And some proof in the pudding:
Not Folded
Folded
Enjoy ;)
wrote on 9 Feb 2021, 15:04 last edited by JonB 2 Sept 2021, 15:05@ATatum_BlurPD
I'm Creator 4.11.0. It works for the folding. Though it's only actually the{
...}
doing that! You can just write#pragma { ... #pragma }
and it folds just the same!
Whatever I put there I get
warning: ignoring #pragma { [-Wunknown-pragmas]
, so I'd have to switch off unknown pragmas warnings.So thank you, but it's obviously an unsupported hack. Which is fine for some but not for others.
- Make sure 'Display Folding markers' is enabled
-
wrote on 30 Mar 2021, 01:08 last edited by Rafiz
Oh, that's pretty fresh topic, and I look for same thing.
In code blocks, sublimetext and many other editors you could write//{ description int i; //(or some other code ...) //}
and than you could collapse that code. But //{ won't work in qtCreator.
I'm not anyone qualified, but I think interpreting comments by editor isn't bad..
(I say so, as it looks qtCreator avoids that? {} after # aren't ignored and are interpreted (which - by the way - is kind of strange in my opinnion - like if I wanted to #define something_to_be { , it doesn't mean I want it to be folding block) )So //{ is something I would suggest - as it's popular solution in other editors.
#pragma and/or #region would be cool too, but I just provide another ideaAs for why I would use such thing - it's just that overloading all operators result in a lot of code, that I have to scroll through, code that I'm more often (than not) - not interested in looking at. Moving overloaded operators implementations to another .cpp is... Weird idea for me. And what is (and what's not) too much code to look through is very personal - so it's not about bad structure of code. For me even 200 lines is too much. I want less file to scroll/search through. Overloading operators use 40 of those lines in my case. And I wrote them caring more about lines used than about readibillity anyway (most of them are two-liners)
For now I think I will resolve to using big eye catching comment lines to know when I need to stop scrolling fast through some segment..
-
Oh, that's pretty fresh topic, and I look for same thing.
In code blocks, sublimetext and many other editors you could write//{ description int i; //(or some other code ...) //}
and than you could collapse that code. But //{ won't work in qtCreator.
I'm not anyone qualified, but I think interpreting comments by editor isn't bad..
(I say so, as it looks qtCreator avoids that? {} after # aren't ignored and are interpreted (which - by the way - is kind of strange in my opinnion - like if I wanted to #define something_to_be { , it doesn't mean I want it to be folding block) )So //{ is something I would suggest - as it's popular solution in other editors.
#pragma and/or #region would be cool too, but I just provide another ideaAs for why I would use such thing - it's just that overloading all operators result in a lot of code, that I have to scroll through, code that I'm more often (than not) - not interested in looking at. Moving overloaded operators implementations to another .cpp is... Weird idea for me. And what is (and what's not) too much code to look through is very personal - so it's not about bad structure of code. For me even 200 lines is too much. I want less file to scroll/search through. Overloading operators use 40 of those lines in my case. And I wrote them caring more about lines used than about readibillity anyway (most of them are two-liners)
For now I think I will resolve to using big eye catching comment lines to know when I need to stop scrolling fast through some segment..
@Rafiz You can file change request in Qt bug tracker: https://bugreports.qt.io/secure/Dashboard.jspa
-
wrote on 1 Feb 2024, 09:41 last edited by
I agree wiht it. folding region could let codes look neat and organized without having to create more meaningless funtions and files and jump between them.
I saw someone use#pragma region RegionName{ #pragma endregion}
to fold codes.and enter text "-Wno-unknow-pragmas" in Tools->Options->C++->Code Model->Build-system warings(Paul)-> Build-system warnings->Copy->(give the configuration name)->(Choose your configuration)->(tick) Use diagnostic flags from build system, to avoid generating warnings.