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?
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));
}
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);
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