作为验证代码实际运行的线程的一种方法,我使用了QThread::currentThreadId()。但是,从该函数返回的Qt::手柄类型是根据文档(依赖于平台的类型)返回的。在我的平台(Linux)上,它只是一个用于void * (无类型指针)的typedef。
那么,我将如何使用例如qDebug()打印这个,以及如何将它转换为一个QString?
发布于 2014-11-01 20:54:06
我自己用以下两种帮助功能修复了这个问题。请注意,我选择使用void *作为类型,而不是使用Qt::HANDLE,因为这在其他情况下和其他平台也可能有用。
//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));
}发布于 2019-08-07 10:51:06
我更喜欢这种方式,也许您可以创建一个qstring变量,然后通过对一些小部件使用这个qstring变量来打印它,甚至可以打印setText()。
QString id=QString( "%1" ).arg(stati_cast<int>(QThread::currentThreadId()), 16);
ui->user->setText(id);
this->setWindowTitle(id);https://stackoverflow.com/questions/26693143
复制相似问题