We have a simple piece of code in our application:
void tAccessPoint::OnStateChanged(QAbstractSocket::SocketState state)
{
qDebug() << m_ID << " " << state;
For reasons that aren't important here I was attempting to replace the use of qDebug so I used the code from this post C++ format macro / inline ostringstream. But I was surprised to find that when I do this state no longer appears as a text value but rather as a numeric value. qDebug() seems to know what the name of the enum value is rather than just the value. How does it do this, and can I do the same in my code?
There is no moc magic here, QtNetwork defines explicitly the operator in network/socket/qabstractsocket.h:
QDebug operator<<(QDebug, QAbstractSocket::SocketState) {
switch (state) {
case QAbstractSocket::UnconnectedState:
debug << "QAbstractSocket::UnconnectedState";
break;
case QAbstractSocket::HostLookupState:
debug << "QAbstractSocket::HostLookupState";
break;
case QAbstractSocket::ConnectingState:
debug << "QAbstractSocket::ConnectingState";
break;
case QAbstractSocket::ConnectedState:
debug << "QAbstractSocket::ConnectedState";
break;
case QAbstractSocket::BoundState:
debug << "QAbstractSocket::BoundState";
break;
...
return debug;
}
But you can use QDebug
to send the data to a QString
inside your function:
QString output;
QDebug(&output) << ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With