Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Qt Creator C++ folding region



  • 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...?


  • Moderators

    @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.


  • Moderators

    @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.



  • @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 #includes 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! ;-)


  • Moderators

    @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

    🤗



  • @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 the Serialization word so I know what is there), and similarly with some other grouped functionality.


  • Moderators

    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.



  • @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... :)



  • @JonB You wil be happy to find out that you can do this!

    Goto tools>options>display>display>see image.

    a624b30a-76ba-4b9a-a3c2-b3d3226ca34b-image.png



  • @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?


  • Moderators

    @JonB said in Qt Creator C++ folding region:

    Doesn't that just determine whether folding markers are visible or not

    Yes, it does.



  • If you encapsulate in { }.



  • @Justin-Pattison-Schmidt
    You can't encapsulate multiple function definitions inside {}, nor lists of #include statements....


  • Moderators

    @JonB you could encapsulate it in #ifdef and #endif but that won't make magically create folding markers either 🤷‍♂️

    When IMHO it actually should!



  • @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!



  • 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}
    
    

    And some proof in the pudding:

    Not Folded

    76c18c84-1194-401c-8424-dbc67a058870-image.png

    Folded

    142a5887-7109-41b0-83f8-8730ee8cd440-image.png

    Enjoy ;)



  • @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.



  • 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 idea

    As 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..


  • Lifetime Qt Champion

    @Rafiz You can file change request in Qt bug tracker: https://bugreports.qt.io/secure/Dashboard.jspa


Log in to reply