13

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

8 Answers 8

12

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() {}
}
0
9

Currently not.

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.

10
  • The humble opinion is the solicited opinion... IMO Commented Aug 27, 2014 at 4:16
  • @DaveCausey: My favourite dictionaries do not translate "solicitied" to something that makes sense in this context. Does it mean "the crowd's opinion"? Commented Aug 30, 2014 at 6:39
  • 1
    Solicited = requested in this context. Just being silly. I spent some time trying to find an answer to a similar question and there seems to be a lot of principled resistance to this practice. While I tend to agree, I have found outliners to be a helpful tool for breaking down pre-existing complexity in third party code when significant refactoring is not an immediate option. Commented Sep 8, 2014 at 10:47
  • So many reasons to do manual block folding... for example, folding across multiple blocks rather than just folding codes inside a pair of curly parenthesis.
    – Robin Hsu
    Commented Dec 16, 2014 at 10:33
  • And the folding algorithm in the editor is not easy, since the code can be complex. #ifdef exists, many places, which can even destroy the pairing of curly parenthesis. Current Qt editor code can not even handle the #if 0 #else #endif case.
    – Robin Hsu
    Commented Dec 16, 2014 at 10:36
7

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 }
1
  • 2
    This is probably the best solution for the problem that we have at the moment. The only problem is that you have to use a unique identifier for each region (otherwise you will receive a warning)
    – Zciurus
    Commented Apr 20, 2021 at 8:47
6

you can place your code in {} and write a comment for its name.

{ // RegionName 
    void Test() {}
    void Test2() {}
    void Test3() {}
}
1
  • I second this over the rest
    – IOviSpot
    Commented Mar 29, 2022 at 18:56
1

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

1

This is very old but still actual. For convenience, you can also add a snippet to automate entering a region of code. Navigate menu to Preferences/Text Editor/Snippets, choose the group 'Text' and insert a new snippet having Trigger = region and Trigger Variant = name, then add the following text in the lower area (add an extra carriage return after the '}'). Now, in code, you can just type 'region' and the snippet will be inserted while the editor will ask to set the 'name' of the region. Only drawback, #pragma region generates a warning but you can suppress it by adding:

QMAKE_CXXFLAGS_WARN_ON += -Wno-unknown-pragmas

to your .pro file. Anyway you can always substitute the snippet with whatever you find works better.

#pragma region $name$ {

#pragma endregion }

Set Preferences...Snippet example image

1

It works this way:

#pragma region REGION1 {

// YOUR CODE

#pragma endregion REGION1 }
1
0

you can place your code in {} and write a comment for its name.

This WILL THROW "EXPECTED UN-QUALIFIED ID" error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.