C++ 如何将指针值转换为 QString?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8881923/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 19:16:20  来源:igfitidea点击:

How to convert a pointer value to QString?

c++qtpointersqstring

提问by Martin Hennings

for debug purposes I often output pointer values (mostly this) to qDebug:

出于调试目的,我经常将指针值(主要是this)输出到 qDebug:

qDebug("pointer of current object = 0x%08x",this);, using "%08x" as format string and simply passing thisas a parameter.

qDebug("pointer of current object = 0x%08x",this);, 使用 "%08x" 作为格式字符串并简单地this作为参数传递。

How can I convert the pointer value to a QString?

如何将指针值转换为 QString?

This is what I got so far:

这是我到目前为止得到的:

char p = (char)this;
return QString("0x%1").arg(p, 8, '0');

But the compiler doesn't seem to figure out what to do with that value. Is casting to charcorrect in this case? Or what would be a safer way to do this?

但是编译器似乎不知道如何处理该值。char在这种情况下是否要纠正?或者什么是更安全的方法来做到这一点?

Using Visual C++ with Qt 4.7.4.

在 Qt 4.7.4 中使用 Visual C++。

EDIT

编辑

Using qulonglongas suggested

qulonglong按照建议使用

qulonglong p = (qulonglong)this;
return QString("0x%1").arg(p, 8, '0');

yields in a compiler error message (error C2666).

在编译器错误消息(错误 C2666)中产生

回答by Ignitor

Using QString::arg():

使用QString::arg()

MyClass *ptr = new MyClass();
QString ptrStr = QString("0x%1").arg((quintptr)ptr, 
                    QT_POINTER_SIZE * 2, 16, QChar('0'));

It will use the correct type and size for pointers (quintptrand QT_POINTER_SIZE) and will always prefix "0x".

它将为指针(quintptrQT_POINTER_SIZE)使用正确的类型和大小,并且始终以 为前缀"0x"

Notes:
To prefix the value with zeros, the fourth parameter needs to be QChar('0').
To output the correct number of digits, QT_POINTER_SIZEneeds to be doubled (because each byte needs 2 hex digits).

注意:
要在值前面加上零,第四个参数必须是QChar('0')
要输出正确的位数,QT_POINTER_SIZE需要加倍(因为每个字节需要 2 个十六进制数字)。

回答by Kamil Klimek

Why not simply use QString & QString::sprintf ( const char * cformat, ... )

为什么不简单地使用 QString & QString::sprintf ( const char * cformat, ... )

QString t;
// QString::sprintf adds 0x prefix
t.sprintf("%08p", ptr);

回答by HostileFork says dont trust SE

QTextStreamappears to offer the functionality you seek, and operates simply on a void*.

QTextStream似乎提供了您所寻求的功能,并且只需在void*.

const void * address = static_cast<const void*>(ptr);
QString addressString;
QTextStream addressStream (&addressString);
addressStream << address;
qDebug() << addressString;

Unlike the other approaches, it does not reference notions like the particular number "8" or casting to "qlonglong". Seems less worrisome, and is much like the prescribed method for std::string(though without getting std::stringconversions involved)

与其他方法不同,它不引用特定数字“8”或强制转换为“qlonglong”等概念。似乎不那么令人担忧,并且很像std::string规定方法(尽管不涉及std::string转换)

回答by Some programmer dude

You could do an intermediate step with sprintf:

你可以做一个中间步骤sprintf

QString pointer_to_qstring(void *ptr)
{
    char temp[16];
    sprintf(temp, "0x%08p", ptr);
    return QString(static_cast<char *>(temp));
}

Or through ostringstream:

或通过ostringstream

QString pointer_to_qstring(void *ptr)
{
    std::ostringstream oss;
    oss << std::setw(8) << std::setfill('0') << std::hex << ptr:
    return QString(oss.str().c_str());
}

回答by newandlost

For me mostly it is enough to see if the pointer is the nullptror if it has the same value as an other pointer. For those cases it is enough to convert the pointer to a number and then use QString::number()

对我来说,主要是查看指针是否与nullptr其他指针具有相同的值。对于这些情况,将指针转换为数字就足够了,然后使用QString::number()

Cat * cat = new Cat();
qDebug()<<QString::number((std::uintptr_t)(cat));;