Is there an easy way to log enums, bool with literal? If so, what does it look like?
-
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;
-
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()
-
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;
@RobertSommer
For "enums to string", I know thatQ_ENUM()
s let you use https://forum.qt.io/post/500356QMetaEnum::fromType<ClassName::MyEnum>().valueToKey(value) // MyEnum needs to be declared with Q_ENUM macro
https://gist.github.com/MoeQatoum/4107191ef11b804f7bf7c12d51fb8321
-
@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.
-
@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.
@RobertSommer
The post about Typescript is not relevant to your question.
As above, you should look at QMetaEnum Class. -
@RobertSommer
The post about Typescript is not relevant to your question.
As above, you should look at QMetaEnum Class.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.
-
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.
@RobertSommer
My answer is for when you useQ_ENUM()
for yourenum
, which I thought was what you were doing. The code you show has noQ_ENUM()
.If you expect something about being able to convert a plain C++
enum
with noQ_ENUM()
then it has nothing to do with Qt. For that you can searchc++ 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