直接看代码
QString str;
if (str.isNull())
{
qDebug() << "isNull1" << endl;
}
if (str.isEmpty())
{
qDebug() << "isEmpty1" << endl;
}
qDebug()<<"***************";
str = "";
if (str.isNull())
{
qDebug() << "isNull2" << endl;
}
if (str.isEmpty())
{
qDebug() << "isEmpty2" << endl;
}
qDebug()<<"***************";
QString str2;
if (str2 == "")
{
qDebug() << "str2 isNull" << endl;
}
输出结果
isNull1
isEmpty1
***************
isEmpty2
***************
str2 isNull
从结果可以看出isEmpty()是用来判断是否是空串(无论初始化与否),isNull()表示是否为空(什么都没有,有没有被初始化),如果要判断是否为空串,直接使用 str == ""比较方便。