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. How to "wrap " text widgets in debug ?
Forum Updated to NodeBB v4.3 + New Features

How to "wrap " text widgets in debug ?

Scheduled Pinned Locked Moved Unsolved General and Desktop
17 Posts 6 Posters 1.1k Views 4 Watching
  • 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.
  • A Offline
    A Offline
    Anonymous_Banned275
    wrote on 6 Feb 2022, 17:06 last edited by
    #1

    I have changed my debugging philosophy - now I post debugging messages in same text widget where a real output goes .

    Now I am looking for a way to control these debug messages.
    ( Yes I how about "debug category " )
    basically replacing this

    #ifdef TRACE
    ui->chat->append("TRACE initialized...") ;
    #endif

    with something like this

    qTRACEDebug(ui->chat->append("TRACE initialized...")) ;

    Of course my syntax is wrong....

    Help solving the syntax would be appreciated.
    Cheers

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 6 Feb 2022, 19:36 last edited by
      #2

      Hi,

      Are you asking how to create a macro that replaces your ifdef ?
      Something like:

      #define TraceDebug(text) \
        #ifdef TRACE \
        ui->chat->append(text); \
        #endif
      

      ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      A 1 Reply Last reply 6 Feb 2022, 22:19
      0
      • S SGaist
        6 Feb 2022, 19:36

        Hi,

        Are you asking how to create a macro that replaces your ifdef ?
        Something like:

        #define TraceDebug(text) \
          #ifdef TRACE \
          ui->chat->append(text); \
          #endif
        

        ?

        A Offline
        A Offline
        Anonymous_Banned275
        wrote on 6 Feb 2022, 22:19 last edited by
        #3

        @SGaist No, I am asking how to correct this syntax

        qTRACEDebug(ui->chat->append("TRACE initialized...")) ;

        so I can use qTRACEDebug to enable / disable output to widget.

        P 1 Reply Last reply 7 Feb 2022, 02:33
        0
        • A Anonymous_Banned275
          6 Feb 2022, 22:19

          @SGaist No, I am asking how to correct this syntax

          qTRACEDebug(ui->chat->append("TRACE initialized...")) ;

          so I can use qTRACEDebug to enable / disable output to widget.

          P Offline
          P Offline
          Pl45m4
          wrote on 7 Feb 2022, 02:33 last edited by Pl45m4 2 Jul 2022, 02:34
          #4

          @AnneRanch

          So you mean a replacement for :

          #ifdef TRACE
          ui->chat->append("TRACE initialized...") ;
          #endif
          

          Your own debug function in a section that only works, when TRACE is enabled?!

          What's wrong with it? AFAIK that, what you are trying to do, wont work this way (with qTraceDebug). Why not keep the 2 lines?


          If debugging is the process of removing software bugs, then programming must be the process of putting them in.

          ~E. W. Dijkstra

          A 1 Reply Last reply 7 Feb 2022, 04:22
          0
          • P Pl45m4
            7 Feb 2022, 02:33

            @AnneRanch

            So you mean a replacement for :

            #ifdef TRACE
            ui->chat->append("TRACE initialized...") ;
            #endif
            

            Your own debug function in a section that only works, when TRACE is enabled?!

            What's wrong with it? AFAIK that, what you are trying to do, wont work this way (with qTraceDebug). Why not keep the 2 lines?

            A Offline
            A Offline
            Anonymous_Banned275
            wrote on 7 Feb 2022, 04:22 last edited by
            #5

            @Pl45m4

            \YES I want to replace
            #ifdef
            .....
            #endif

            I am looking for a solution to my wrong syntax, not for
            an excuse for NOT to do it .
            Please stay focused..

            Here is how QDebug is used now
            qDebug ( "TEST " ) ;
            or
            qDebug () << "TEST";

            I can disable both styles by

            DEFINES += QT_NO_DEBUG_OUTPUT

            what I want is to have similar way to debug text widget. qDebug is a macro - all I want is to add ui->chat->append("TRACE initialized...")) ; to this macro. I do not know how.....

            1 Reply Last reply
            0
            • C Offline
              C Offline
              ChrisW67
              wrote on 7 Feb 2022, 04:34 last edited by
              #6

              So, you want code to appear when a preprocessor macro is defined, and totally disappear when that macro is undefined. Common requirement with a common solution:

              #define TRACE 1
              
              #ifdef TRACE 
                  #define qTRACEDebug(RANDOMCODE) RANDOMCODE ;
              #else
                  #define qTRACEDebug(RANDOMCODE) 
              # endif
              
              // In one place
              qTRACEDebug(ui->chat->append("TRACE initialized..."))
              
              // In another
              qTRACEDebug(ui->blah->append("TRACE in the blah area..."))
              

              Experiment here: https://godbolt.org/z/3j8sacj4K

              Then all you need to be careful about is that "RANDOMCODE" does not break macro expansion.

              P 1 Reply Last reply 7 Feb 2022, 15:04
              3
              • C ChrisW67
                7 Feb 2022, 04:34

                So, you want code to appear when a preprocessor macro is defined, and totally disappear when that macro is undefined. Common requirement with a common solution:

                #define TRACE 1
                
                #ifdef TRACE 
                    #define qTRACEDebug(RANDOMCODE) RANDOMCODE ;
                #else
                    #define qTRACEDebug(RANDOMCODE) 
                # endif
                
                // In one place
                qTRACEDebug(ui->chat->append("TRACE initialized..."))
                
                // In another
                qTRACEDebug(ui->blah->append("TRACE in the blah area..."))
                

                Experiment here: https://godbolt.org/z/3j8sacj4K

                Then all you need to be careful about is that "RANDOMCODE" does not break macro expansion.

                P Offline
                P Offline
                Pl45m4
                wrote on 7 Feb 2022, 15:04 last edited by Pl45m4 2 Jul 2022, 15:05
                #7

                @ChrisW67

                Wouldnt that lead to errors when TRACE is not defined and you still have qTRACEDebug in your code?!
                (because there's no TraceDebug?!)


                If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                ~E. W. Dijkstra

                C 1 Reply Last reply 8 Feb 2022, 05:50
                0
                • A Offline
                  A Offline
                  Anonymous_Banned275
                  wrote on 7 Feb 2022, 17:36 last edited by Anonymous_Banned275 2 Jul 2022, 17:56
                  #8

                  I need to look at the definition of qDebug macro...
                  ( I need more time to do so )
                  I was not looking for replacing qDebug macro with another one.

                  Here is SOME explanation of qDebug macro

                  https://stackoverflow.com/questions/34690328/how-does-qtdebug-syntax-work
                  That leads me to believe IT shud be able to get addition l info from qDebug -0 such as "line #" where qDebug is codded... maybe later...

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    Anonymous_Banned275
                    wrote on 7 Feb 2022, 18:39 last edited by
                    #9

                    Update

                    It looks that the original syntax with " <<" should work.
                    It works with "tab widget": , but fails with
                    "text edit"

                    What is the difference ?

                    qDebug() << "TEST qDebug: " << ui->tabWidget->tabText(testIndex ).append("TEST"); WORKS FINE
                    qDebug() << "TEST qDebug: " << ui->tabWidget->tabText(testIndex );
                    qDebug() << "TEST qDebug: " << ui->textEdit;

                    qDebug() << "TEST qDebug: " << ui->textEdit->append("TEST TRACE debug "); FAILS

                    /media/q5/MDI/QT_PROGRAMS/LoCAL_SOURCE/HCI/form.cpp:513: error: invalid operands to binary expression ('QDebug' and 'void')

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on 7 Feb 2022, 19:40 last edited by
                      #10

                      tabText returns a QString.
                      append returns void.

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      2
                      • M Offline
                        M Offline
                        mpergand
                        wrote on 7 Feb 2022, 20:06 last edited by mpergand 2 Jul 2022, 20:08
                        #11
                        #define WidgetLogEnabled
                        
                        #ifdef WidgetLogEnabled
                        #define Log(text,widget)  widget->append(text); \
                                qDebug()<<text      // log to the console also
                        #else
                        #define Log(text,widget) qDebug()<<text
                        #endif
                        
                        A 1 Reply Last reply 7 Feb 2022, 23:47
                        2
                        • M mpergand
                          7 Feb 2022, 20:06
                          #define WidgetLogEnabled
                          
                          #ifdef WidgetLogEnabled
                          #define Log(text,widget)  widget->append(text); \
                                  qDebug()<<text      // log to the console also
                          #else
                          #define Log(text,widget) qDebug()<<text
                          #endif
                          
                          A Offline
                          A Offline
                          Anonymous_Banned275
                          wrote on 7 Feb 2022, 23:47 last edited by
                          #12

                          @mpergand Thank you for providing the solution to the "text" widget.
                          I was bedding to wonder if the whole idea needs to be ditched.
                          However, I have learn long time ago that "functions" better return something - just to keep code honest. My opinion is that this kind of "hiding" or not returning anything was not part of the OOP. ( Just an opinion,,,)
                          Cheers

                          M 1 Reply Last reply 8 Feb 2022, 12:07
                          0
                          • P Pl45m4
                            7 Feb 2022, 15:04

                            @ChrisW67

                            Wouldnt that lead to errors when TRACE is not defined and you still have qTRACEDebug in your code?!
                            (because there's no TraceDebug?!)

                            C Offline
                            C Offline
                            ChrisW67
                            wrote on 8 Feb 2022, 05:50 last edited by ChrisW67 2 Aug 2022, 05:50
                            #13

                            @Pl45m4 No. If TRACE is defined then the macro is defined to expand to the content of the macro argument, and if TRACE is undef then the preprocessor macro is defined to be a no-op. Either way, the entire macro is replaced in the output. So this:

                            foo;
                            qTRACEDebug(ui->chat->append("TRACE initialized..."))
                            bar;
                            

                            where the entire second line is the macro, becomes this after the pre-processor stage:

                            foo;
                            
                            bar;
                            

                            This achieves exactly what the OP asked for which was, as usual, not what the OP actually wanted.

                            J 1 Reply Last reply 8 Feb 2022, 07:51
                            3
                            • C ChrisW67
                              8 Feb 2022, 05:50

                              @Pl45m4 No. If TRACE is defined then the macro is defined to expand to the content of the macro argument, and if TRACE is undef then the preprocessor macro is defined to be a no-op. Either way, the entire macro is replaced in the output. So this:

                              foo;
                              qTRACEDebug(ui->chat->append("TRACE initialized..."))
                              bar;
                              

                              where the entire second line is the macro, becomes this after the pre-processor stage:

                              foo;
                              
                              bar;
                              

                              This achieves exactly what the OP asked for which was, as usual, not what the OP actually wanted.

                              J Offline
                              J Offline
                              JonB
                              wrote on 8 Feb 2022, 07:51 last edited by JonB 2 Aug 2022, 07:59
                              #14

                              @ChrisW67
                              Yes, but one tiny, brief point. I would rather you defined your #else case as, say, #define qTRACEDebug(RANDOMCODE) ; or made both routes use {...}. Cf. typical definitions of assert-type macros. At the moment your code means that

                              if (condition)
                                  qTRACEDebug(...)
                              ...
                              

                              succeeds when TRACE is defined but syntax errors, or worse has unexpected behaviour, when it is not?

                              S 1 Reply Last reply 8 Feb 2022, 08:10
                              1
                              • J JonB
                                8 Feb 2022, 07:51

                                @ChrisW67
                                Yes, but one tiny, brief point. I would rather you defined your #else case as, say, #define qTRACEDebug(RANDOMCODE) ; or made both routes use {...}. Cf. typical definitions of assert-type macros. At the moment your code means that

                                if (condition)
                                    qTRACEDebug(...)
                                ...
                                

                                succeeds when TRACE is defined but syntax errors, or worse has unexpected behaviour, when it is not?

                                S Offline
                                S Offline
                                SGaist
                                Lifetime Qt Champion
                                wrote on 8 Feb 2022, 08:10 last edited by
                                #15

                                @JonB While you are correct with regards to the use of {}, I would say that the real issue here is not using the infrastructure that is already available.

                                Using a custom message handler that will then feed the widget with all the debug messages (or only a subset of one) would avoid making the code messy with that kind of stuff. This would also allow to have one dedicated text widget that would show the messages rather than mixing standard and debug messages in the GUI but that's an other topic.

                                Interested in AI ? www.idiap.ch
                                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                1 Reply Last reply
                                2
                                • A Anonymous_Banned275
                                  7 Feb 2022, 23:47

                                  @mpergand Thank you for providing the solution to the "text" widget.
                                  I was bedding to wonder if the whole idea needs to be ditched.
                                  However, I have learn long time ago that "functions" better return something - just to keep code honest. My opinion is that this kind of "hiding" or not returning anything was not part of the OOP. ( Just an opinion,,,)
                                  Cheers

                                  M Offline
                                  M Offline
                                  mpergand
                                  wrote on 8 Feb 2022, 12:07 last edited by mpergand 2 Aug 2022, 17:19
                                  #16

                                  @AnneRanch said in How to "wrap " text widgets in debug ?:

                                  I was bedding to wonder if the whole idea needs to be ditched.

                                  I'm very closed to think the same ;)

                                  If you want special logging mecanism, you can use a dedicated widget for that or save them to a file that you can consult later on.

                                  A 1 Reply Last reply 8 Feb 2022, 17:37
                                  0
                                  • M mpergand
                                    8 Feb 2022, 12:07

                                    @AnneRanch said in How to "wrap " text widgets in debug ?:

                                    I was bedding to wonder if the whole idea needs to be ditched.

                                    I'm very closed to think the same ;)

                                    If you want special logging mecanism, you can use a dedicated widget for that or save them to a file that you can consult later on.

                                    A Offline
                                    A Offline
                                    Anonymous_Banned275
                                    wrote on 8 Feb 2022, 17:37 last edited by
                                    #17

                                    @mpergand Yes, decided against the whole idea "interleaving" debug trace with output. It actually came as a way to bypass QtDesigner "break layout" .
                                    Doing so I always come up with weird, unwanted layout.
                                    Now I do copy the original layout and can recover...
                                    Now I put output in one tab and debug TRACE in another. Then user can choose to observe the TRACE if he desires so.
                                    I still think the qDebug is a decent tool and needs for additional preprocessor macros is debatable..

                                    Cheers

                                    1 Reply Last reply
                                    0

                                    10/17

                                    7 Feb 2022, 19:40

                                    • Login

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