Len*_*and 4 c++ linux multithreading qdebug qt5
作为验证我的代码实际在哪个线程下运行的方法,我使用QThread::currentThreadId()。然而,根据文档,从此函数返回的Qt::HANDLE类型是平台相关的 typedef。void *在我的平台(Linux)上,它只是(无类型指针)的 typedef 。
那么我将如何使用例如打印它qDebug(),以及如何将其转换为 a QString?
我自己用以下两个帮助函数修复了这个问题。请注意,我选择使用void *as 类型而不是Qt::HANDLE因为这在其他情况和其他平台上也可能有用。
// 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));
}
Run Code Online (Sandbox Code Playgroud)