Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does qDebug() print enum values?

Tags:

qt

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?

like image 872
parsley72 Avatar asked Aug 29 '12 09:08

parsley72


1 Answers

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) << ...
like image 137
alexisdm Avatar answered Oct 18 '22 20:10

alexisdm