How does qDebug() print enum values?

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

Maybe this enum to QString convert can be useful :

const QMetaObject & mo = QAbstractSocket::staticMetaObject;
QMetaEnum me = mo.enumerator(mo.indexOfEnumerator("SocketState"));
QString test(me.valueToKey(QAbstractSocket::UnconnectedState));

Tags:

Qt