Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Qt Creator C++ folding region

Qt Creator C++ folding region

Scheduled Pinned Locked Moved Solved General and Desktop
19 Posts 9 Posters 5.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    JonB
    wrote on 3 Mar 2020, 09:01 last edited by JonB 3 Mar 2020, 09:02
    #1

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

    K 1 Reply Last reply 3 Mar 2020, 10:13
    0
    • J JonB
      3 Mar 2020, 09:01

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

      K Offline
      K Offline
      kshegunov
      Moderators
      wrote on 3 Mar 2020, 10:13 last edited by
      #2

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

      Read and abide by the Qt Code of Conduct

      J 1 Reply Last reply 3 Mar 2020, 10:40
      2
      • K kshegunov
        3 Mar 2020, 10:13

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

        J Offline
        J Offline
        JonB
        wrote on 3 Mar 2020, 10:40 last edited by
        #3

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

        J 1 Reply Last reply 3 Mar 2020, 10:43
        0
        • J JonB
          3 Mar 2020, 10:40

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

          J Offline
          J Offline
          J.Hilk
          Moderators
          wrote on 3 Mar 2020, 10:43 last edited by
          #4

          @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

          🤗


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          J 1 Reply Last reply 3 Mar 2020, 10:48
          0
          • J J.Hilk
            3 Mar 2020, 10:43

            @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 Offline
            J Offline
            JonB
            wrote on 3 Mar 2020, 10:48 last edited by JonB 3 Mar 2020, 10:50
            #5

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

            K J 2 Replies Last reply 3 Mar 2020, 10:55
            1
            • J JonB
              3 Mar 2020, 10:48

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

              K Offline
              K Offline
              kshegunov
              Moderators
              wrote on 3 Mar 2020, 10:55 last edited by
              #6

              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.

              Read and abide by the Qt Code of Conduct

              J 1 Reply Last reply 3 Mar 2020, 11:00
              0
              • K kshegunov
                3 Mar 2020, 10:55

                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.

                J Offline
                J Offline
                JonB
                wrote on 3 Mar 2020, 11:00 last edited by
                #7

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

                1 Reply Last reply
                0
                • J JonB
                  3 Mar 2020, 10:48

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

                  J Offline
                  J Offline
                  JoeJoe_000
                  wrote on 3 Mar 2020, 19:07 last edited by
                  #8

                  @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

                  J 1 Reply Last reply 3 Mar 2020, 19:52
                  0
                  • J JoeJoe_000
                    3 Mar 2020, 19:07

                    @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

                    J Offline
                    J Offline
                    JonB
                    wrote on 3 Mar 2020, 19:52 last edited by
                    #9

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

                    K 1 Reply Last reply 3 Mar 2020, 20:36
                    0
                    • J JonB
                      3 Mar 2020, 19:52

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

                      K Offline
                      K Offline
                      kshegunov
                      Moderators
                      wrote on 3 Mar 2020, 20:36 last edited by
                      #10

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

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

                      Yes, it does.

                      Read and abide by the Qt Code of Conduct

                      1 Reply Last reply
                      1
                      • J Offline
                        J Offline
                        Justin Pattison-Schmidt
                        wrote on 4 Mar 2020, 22:14 last edited by
                        #11

                        If you encapsulate in { }.

                        J 1 Reply Last reply 5 Mar 2020, 08:20
                        0
                        • J Justin Pattison-Schmidt
                          4 Mar 2020, 22:14

                          If you encapsulate in { }.

                          J Offline
                          J Offline
                          JonB
                          wrote on 5 Mar 2020, 08:20 last edited by JonB 3 May 2020, 08:21
                          #12

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

                          J 1 Reply Last reply 5 Mar 2020, 08:24
                          0
                          • J JonB
                            5 Mar 2020, 08:20

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

                            J Offline
                            J Offline
                            J.Hilk
                            Moderators
                            wrote on 5 Mar 2020, 08:24 last edited by
                            #13

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

                            When IMHO it actually should!


                            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                            Q: What's that?
                            A: It's blue light.
                            Q: What does it do?
                            A: It turns blue.

                            J 1 Reply Last reply 5 Mar 2020, 08:39
                            0
                            • J J.Hilk
                              5 Mar 2020, 08:24

                              @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 Offline
                              J Offline
                              JonB
                              wrote on 5 Mar 2020, 08:39 last edited by JonB 3 May 2020, 08:39
                              #14

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

                              1 Reply Last reply
                              1
                              • A Offline
                                A Offline
                                ATatum_BlurPD
                                wrote on 9 Feb 2021, 14:37 last edited by
                                #15

                                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 ;)

                                J 1 Reply Last reply 9 Feb 2021, 15:04
                                7
                                • A ATatum_BlurPD
                                  9 Feb 2021, 14:37

                                  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 ;)

                                  J Offline
                                  J Offline
                                  JonB
                                  wrote on 9 Feb 2021, 15:04 last edited by JonB 2 Sept 2021, 15:05
                                  #16

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

                                  1 Reply Last reply
                                  1
                                  • R Offline
                                    R Offline
                                    Rafiz
                                    wrote on 30 Mar 2021, 01:08 last edited by Rafiz
                                    #17

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

                                    jsulmJ 1 Reply Last reply 30 Mar 2021, 05:13
                                    0
                                    • R Rafiz
                                      30 Mar 2021, 01:08

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

                                      jsulmJ Offline
                                      jsulmJ Offline
                                      jsulm
                                      Lifetime Qt Champion
                                      wrote on 30 Mar 2021, 05:13 last edited by
                                      #18

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

                                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                                      1 Reply Last reply
                                      0
                                      • Q Offline
                                        Q Offline
                                        Qtcpp_User
                                        wrote on 1 Feb 2024, 09:41 last edited by
                                        #19

                                        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.

                                        1 Reply Last reply
                                        1

                                        • Login

                                        • Login or register to search.
                                        • First post
                                          Last post
                                        0
                                        • Categories
                                        • Recent
                                        • Tags
                                        • Popular
                                        • Users
                                        • Groups
                                        • Search
                                        • Get Qt Extensions
                                        • Unsolved