简体   繁体   中英

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 ?

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::HADNLE 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::HADNLE 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(stati_cast<int>(QThread::currentThreadId()), 16);
ui->user->setText(id);
this->setWindowTitle(id);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM