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. Is there an easy way to log enums, bool with literal? If so, what does it look like?
QtWS25 Last Chance

Is there an easy way to log enums, bool with literal? If so, what does it look like?

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 4 Posters 110 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.
  • R Offline
    R Offline
    RobertSommer
    wrote last edited by
    #1

    Hello,
    Is there an easy way to log enums, bool with literal?

    Like 
          bool test = false;
    	  test.ToStrng()	  
    	  SocketState myState;
    	  myStatetoString();	  	  
    
    	  Q_ENUM(SocketError)
        enum SocketState {
            UnconnectedState,
            HostLookupState,
            ConnectingState,
            ConnectedState,
            BoundState,
            ListeningState,
            ClosingState
        };
    	
    	QString help = QString("[CUSTOM][ToInk] initialized= '%1'    socket->state()= '%2'")
                .arg(initialized ? "true" : "false")
                .arg(socket->state());
        qDebug() << help;
    
    JonBJ 1 Reply Last reply
    0
    • C Offline
      C Offline
      CassD
      wrote last edited by CassD
      #2

      hi

      hard to understand.

      the boolean type has no toString method as boolean is not a class (and same for int, float and enums)

      what you can try is :

      bool someBool = false ;
      qDebug() << someBool ; // Outputs false
      
      enum SomeEnum { Value1, Value2, Value3, Value4 } ;
      SomeEnum value = Value2 ;
      qDebug() << value ;
      
      

      Otherwise if you need to build a QString that is not intended to be (directly) printed to the console, you can watch QString::arg(), QString::number()

      1 Reply Last reply
      0
      • A Offline
        A Offline
        alina11
        Banned
        wrote last edited by
        #3
        This post is deleted!
        R 1 Reply Last reply
        0
        • R RobertSommer

          Hello,
          Is there an easy way to log enums, bool with literal?

          Like 
                bool test = false;
          	  test.ToStrng()	  
          	  SocketState myState;
          	  myStatetoString();	  	  
          
          	  Q_ENUM(SocketError)
              enum SocketState {
                  UnconnectedState,
                  HostLookupState,
                  ConnectingState,
                  ConnectedState,
                  BoundState,
                  ListeningState,
                  ClosingState
              };
          	
          	QString help = QString("[CUSTOM][ToInk] initialized= '%1'    socket->state()= '%2'")
                      .arg(initialized ? "true" : "false")
                      .arg(socket->state());
              qDebug() << help;
          
          JonBJ Offline
          JonBJ Offline
          JonB
          wrote last edited by
          #4

          @RobertSommer
          For "enums to string", I know that Q_ENUM()s let you use https://forum.qt.io/post/500356

          QMetaEnum::fromType<ClassName::MyEnum>().valueToKey(value) // MyEnum needs to be declared with Q_ENUM macro

          https://gist.github.com/MoeQatoum/4107191ef11b804f7bf7c12d51fb8321

          1 Reply Last reply
          0
          • A alina11

            This post is deleted!

            R Offline
            R Offline
            RobertSommer
            wrote last edited by
            #5

            @alina11 said in Is there an easy way to log enums, bool with literal? If so, what does it look like?:

            // Logging the status and value
            console.log(User active: ${isUserActive}, Current status: ${currentStatus});
            In this case, the Status enum gives you clear names for your values, and you can directly log the values or boolean status using template literals for better readability.

            Let me know if you need help with a specific language or a more advanced logging approach!

            I do not use a scripting language.

            Does your suggestion also work for QT C++?

            console.log(User active: ${isUserActive}, Current status: ${currentStatus});

            looks like C#. Is this also possible in C++? or only via QString and arg.

            JonBJ 1 Reply Last reply
            0
            • R RobertSommer

              @alina11 said in Is there an easy way to log enums, bool with literal? If so, what does it look like?:

              // Logging the status and value
              console.log(User active: ${isUserActive}, Current status: ${currentStatus});
              In this case, the Status enum gives you clear names for your values, and you can directly log the values or boolean status using template literals for better readability.

              Let me know if you need help with a specific language or a more advanced logging approach!

              I do not use a scripting language.

              Does your suggestion also work for QT C++?

              console.log(User active: ${isUserActive}, Current status: ${currentStatus});

              looks like C#. Is this also possible in C++? or only via QString and arg.

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote last edited by JonB
              #6

              @RobertSommer
              The post about Typescript is not relevant to your question.
              As above, you should look at QMetaEnum Class.

              R 1 Reply Last reply
              0
              • JonBJ JonB

                @RobertSommer
                The post about Typescript is not relevant to your question.
                As above, you should look at QMetaEnum Class.

                R Offline
                R Offline
                RobertSommer
                wrote last edited by
                #7

                @JonB

                enum State
                {
                    Notdefine,
                    StandBy,
                    Error,
                    Automatic
                };
                
                    State testEnum;
                    testEnum = State::Error;
                    qDebug() << "#### Incoming data #### " << testEnum << "  "; //<< testEnum.StateToString(testEnum.State);
                
                 //--> Output is 2025-05-07 11:31:34.988 - #### Incoming data ####  2
                 
                   QMetaEnum metaEnum = QMetaEnum::fromType<State>();
                   QString t3 =  metaEnum.valueToKey(testEnum);
                   
                //   C:\Qt\include\QtCore\qmetaobject.h:236: error: C2665: "qt_getEnumName": None of the 15 overloads were able to convert all argument types.
                
                //  Error: Cannot read C:/SVN/My_1/trunk/2369052/Test/deployment.pri: No such file or directory
                

                I have a small enum, I just want to log it in plain text in the log file.

                JonBJ 1 Reply Last reply
                0
                • R RobertSommer

                  @JonB

                  enum State
                  {
                      Notdefine,
                      StandBy,
                      Error,
                      Automatic
                  };
                  
                      State testEnum;
                      testEnum = State::Error;
                      qDebug() << "#### Incoming data #### " << testEnum << "  "; //<< testEnum.StateToString(testEnum.State);
                  
                   //--> Output is 2025-05-07 11:31:34.988 - #### Incoming data ####  2
                   
                     QMetaEnum metaEnum = QMetaEnum::fromType<State>();
                     QString t3 =  metaEnum.valueToKey(testEnum);
                     
                  //   C:\Qt\include\QtCore\qmetaobject.h:236: error: C2665: "qt_getEnumName": None of the 15 overloads were able to convert all argument types.
                  
                  //  Error: Cannot read C:/SVN/My_1/trunk/2369052/Test/deployment.pri: No such file or directory
                  

                  I have a small enum, I just want to log it in plain text in the log file.

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote last edited by
                  #8

                  @RobertSommer
                  My answer is for when you use Q_ENUM() for your enum, which I thought was what you were doing. The code you show has no Q_ENUM().

                  If you expect something about being able to convert a plain C++ enum with no Q_ENUM() then it has nothing to do with Qt. For that you can search c++ enum to string to discover (C++ does not offer it natively).

                  Other than that I don't know about your compiler error. Did you try the single statement code I quoted? Note how the comment there did say:

                  // MyEnum needs to be declared with Q_ENUM macro

                  1 Reply Last reply
                  0

                  • Login

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