c++linuxmultithreadingqt5qdebug

How can I print Qt:HANDLE on linux? (Qt5)


As a means to verify which thread my code is actually running under I use QThread::currentThreadId(). However the Qt::HANDLE type that is returned from this function is according to the documentation a platform dependant typedef. On my platform (Linux) it was simply a typedef for void * (typeless pointer).

So how would I go about printing this using for example qDebug(), and how about converting it to a QString?


Solution

  • I fixed this myself with the following two helping functions. Note that I opted for using void * as the type instead of Qt::HANDLE as this might be useful in other cases and other platforms as well.

    // Allow Qt::HANDLE and void * to be streamed to QDebug for easier threads debugging
    QDebug operator <<(QDebug d, void *p){
        d.nospace() << QString::number((long long)p, 16);
        return d.space();
    }
    
    // Allow Qt::HANDLE and void * to be added together with QString objects for easier threads debugging
    const QString operator+ ( const QString &s, void *p ){
        return (s+ QString::number((long long)p, 16));
    }