C++ 如何将 QString 初始化为 null?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9724966/
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
how to initialize a QString to null?
提问by user1065969
What is the difference between QString::number(0)
and ((const char*) 0)
?
QString::number(0)
和 和有((const char*) 0)
什么区别?
I want to initialize a QString
say phoneNumber
to null
. Will phoneNumber(QString::number(0))
and phoneNumber((const char*) 0)
both work?
我想初始化一个QString
说phoneNumber
来null
。会phoneNumber(QString::number(0))
和phoneNumber((const char*) 0)
两者都有效吗?
回答by Michael Burr
To create a null QString
just default initialize it:
要创建一个空值,QString
只需默认初始化它:
QString phoneNumber;
// or if you already have a QString variable and want to 'clear' it:
phoneNumber = QString();
Note that QString::number(0)
is decidedly notnull - it creates a QString with the value "0"
.
请注意,QString::number(0)
它绝对不是null - 它创建了一个 QString 的值"0"
。
You could also initialize the QString
with a NULL
pointer, but I wouldn't recommend it unless you're passing a pointer regardless of whether it's NULL
or not (i.e., it could sometimes point to a C string) since it's unnecessary.
你也可以QString
用一个NULL
指针来初始化它,但我不会推荐它,除非你传递一个指针,不管它是否是NULL
(即,它有时可能指向一个 C 字符串),因为它是不必要的。
You should also understand the following Qt docs:
您还应该了解以下 Qt 文档:
Distinction Between Null and Empty Strings
For historical reasons,
QString
distinguishes between a null string and an empty string. A null string is a string that is initialized usingQString
's default constructor or by passing(const char *)0
to the constructor. An empty string is any string with size 0. A null string is always empty, but an empty string isn't necessarily null:QString().isNull(); // returns true QString().isEmpty(); // returns true QString("").isNull(); // returns false QString("").isEmpty(); // returns true QString("abc").isNull(); // returns false QString("abc").isEmpty(); // returns false
All functions except
isNull()
treat null strings the same as empty strings. For example,toAscii().constData()
returns a pointer to a'\0'
character for a null string (not a null pointer), andQString()
compares equal toQString("")
. We recommend that you always use theisEmpty()
function and avoidisNull()
.
空字符串和空字符串的区别
由于历史原因,
QString
区分空字符串和空字符串。空字符串是使用QString
的默认构造函数或通过传递(const char *)0
给构造函数初始化的字符串。空字符串是大小为 0 的任何字符串。空字符串始终为空,但空字符串不一定为空:QString().isNull(); // returns true QString().isEmpty(); // returns true QString("").isNull(); // returns false QString("").isEmpty(); // returns true QString("abc").isNull(); // returns false QString("abc").isEmpty(); // returns false
除了
isNull()
将空字符串视为空字符串之外的所有函数。例如,toAscii().constData()
返回指向'\0'
空字符串(不是空指针)的字符的指针,并QString()
比较等于QString("")
。我们建议您始终使用该isEmpty()
功能并避免使用isNull()
.
回答by immerhart
#include <QCoreApplication>
#include <QString>
#include <QDebug>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QString name(QString::null);
// or QString name = QString::null;
// or QString name;
qDebug() << name.isNull();
qDebug() << name;
return a.exec();
}
Output:
输出:
true
""
Michael Burr`s solution is of course also correct... But I like the QString::null
more.
Michael Burr 的解决方案当然也是正确的……但我QString::null
更喜欢。