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

Horst Walter
  • 13,663
  • 32
  • 126
  • 228

7 Answers7

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() {}
}
Sheridan
  • 615
  • 1
  • 8
  • 21
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.

Sebastian Mach
  • 38,570
  • 8
  • 95
  • 130
  • The humble opinion is the solicited opinion... IMO – Dave Causey Aug 27 '14 at 04:16
  • @DaveCausey: My favourite dictionaries do not translate "solicitied" to something that makes sense in this context. Does it mean "the crowd's opinion"? – Sebastian Mach Aug 30 '14 at 06: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. – Dave Causey Sep 08 '14 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 Dec 16 '14 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 Dec 16 '14 at 10:36
  • @RobinHsu: Most of well structured code does not have multiple blocks that are so large that you want to fold them. – Sebastian Mach Dec 16 '14 at 11:59
  • By folding codes, it's easier to read codes. It's the reader's freedom to fold from anywhere to anywhere... For example, if there are so many small functions, and I know I already understand them, I would like to fold them, so they will not affect my reading/editing. – Robin Hsu Dec 17 '14 at 10:08
  • @RobinHsu: The problem with this is that there is no clear definition of where to cut the line. Your folds might not equal a fellow programmer's folds, and suddenly all the code get's tainted with folds, checked into your version control system. If code is written in a readable, you do not have to look up functions permanently; and you can use your editor's navigation facilities to jump to functions or lines. Personally, I find folded code harder to read, because it's suddenly invisible, and I have to click here and there to make it visible again. This boils down to a zero sum at best. – Sebastian Mach Dec 17 '14 at 11:30
  • It might be. But if you've ever seen a big C++ project, there are some code blocks that are absolutely required, yet not foldable and impact readability. For example: static members definitions and initialization. My embedded code has a lot of static members, they all have to be initialized in consistent blocks, yet that's a wall of (unfoldable) text beside methods. BTW, they can't be moved outside the main context. – Harry Jan 07 '23 at 12:42
  • @Harry: Personally, if I find code hard to read, I try to counter that by improving the readability by different means, e.g. refactoring such that the class declaration becomes smaller. Also, I find it helps a lot to have a certain order within the class declarations, e.g. constants and statics first, public first. One thing I really like in C++ is the way you declare visibility, add a "public:" there, and it is a public _block_ - this provokes a clear structure (opposed to langs. where members _separately_ have visibility/access declared (which is the reason why I _do_ use #regions there :)) – Sebastian Mach Jan 09 '23 at 09:07
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 }
TerribleDog
  • 1,237
  • 1
  • 8
  • 31
  • 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 Apr 20 '21 at 08:47
6

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

{ // RegionName 
    void Test() {}
    void Test2() {}
    void Test3() {}
}
Kaveh Safavi
  • 499
  • 6
  • 5
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

Maurizio
  • 11
  • 4
0

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

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

Nikaido
  • 4,443
  • 5
  • 30
  • 47