[英]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() . 作为验证我的代码实际在哪个线程下运行的一种方法,我使用QThread :: currentThreadId() 。 However the Qt::HANDLE type that is returned from this function is according to the documentation a platform dependant typedef.
但是,根据文档,从该函数返回的Qt :: HANDLE类型是平台相关的typedef。 On my platform (Linux) it was simply a typedef for
void *
(typeless pointer). 在我的平台(Linux)上,它只是
void *
(无类型指针)的typedef。
So how would I go about printing this using for example qDebug()
, and how about converting it to a QString
? 那么我将如何使用例如
qDebug()
进行打印,以及如何将其转换为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. 请注意,我选择使用
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));
}
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变量,然后可以通过对某些小部件使用此qstring变量来打印它甚至setText()。
QString id=QString( "%1" ).arg(stati_cast<int>(QThread::currentThreadId()), 16);
ui->user->setText(id);
this->setWindowTitle(id);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.