Code folding (#pragma region) in Qt creator
-
18-06-2021 - |
Question
Is there something similar to explicit code regions for folding in Qt Creator:
#pragma region Region_1
void Test() {}
void Test2() {}
void Test3() {}
#pragma endregion Region_1
I can see folding for logical code blocks, but do not know how to explicitly set such a block. My version of Qt Creator is 2.4.1
La solution
I think it is better to structure your code by using code anyways. The regions as also found in C# are imho a bad substitute for proper structuring and keeping things maintainable.
Autres conseils
I think you can do this:
Reformat your someclass.cpp
namespace ns
{
CClass::CClass() {}
CClass::~CClass() {}
void CClass::Test() {}
void CClass::Test2() {}
void CClass::Test3() {}
}
for example as
namespace ns // construction-destruction
{
CClass::CClass() {}
CClass::~CClass() {}
}
namespace ns // test-region
{
void CClass::Test() {}
void CClass::Test2() {}
void CClass::Test3() {}
}
you can place your code in {} and write a comment for its name.
{ // RegionName
void Test() {}
void Test2() {}
void Test3() {}
}
Now we can do this by:
Right before the block that you want to fold you place the following define:
#define FOLDINGSTART {
and directly after the block you place:
#define FOLDINGEND }
This is a bit old, I know, but it showed up when I myself was searching for a resolution to this problem so...
@ATatum_BlurPD said in Qt Creator C++ folding region:
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
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}
I don't have enough reputation here to post image-proof, but you can see them here
you can place your code in {} and write a comment for its name.
This WILL THROW "EXPECTED UN-QUALIFIED ID"
error.