4

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?

Mr. Developerdude
  • 9,118
  • 10
  • 57
  • 95

2 Answers2

5

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));
}
Mr. Developerdude
  • 9,118
  • 10
  • 57
  • 95
2

I prefer this way, maybe you create a qstring variable and then you can print it even setText() by using this qstring variable for some widgets.

QString id=QString( "%1" ).arg(static_cast<int>(QThread::currentThreadId()), 16);
ui->user->setText(id);
this->setWindowTitle(id);
Mr. Developerdude
  • 9,118
  • 10
  • 57
  • 95
xiaowen
  • 41
  • 5
  • Hi! I was explicitly looking for how to print the handle with qDebug(), but your solution is definitely useful for converting to QString! Welcome to Stack Overflow :) – Mr. Developerdude Aug 07 '19 at 12:26