Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to initialize a QString to null?

Tags:

c++

qstring

What is the difference between QString::number(0) and ((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?

like image 368
user1065969 Avatar asked Mar 15 '12 17:03

user1065969


People also ask

How do you initialize QString?

Generally speaking: If you want to initialize a QString from a char*, use QStringLiteral . If you want to pass it to a method, check if that method has an overload for QLatin1String - if yes you can use that one, otherwise fall back to QStringLiteral .

Can we initialise string with null?

Initializing a string variable to null simply means that it does not point to any string object. String s=null; Whereas initializing a string variable to “” means that the variable points to an object who's value is “”.


2 Answers

To create a null QString just default initialize it:

QString phoneNumber;

// or if you already have a QString variable and want to 'clear' it:

phoneNumber = QString();

Note that QString::number(0) is decidedly not null - it creates a QString with the value "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.

You should also understand the following Qt docs:

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 using QString'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), and QString() compares equal to QString(""). We recommend that you always use the isEmpty() function and avoid isNull().

like image 165
Michael Burr Avatar answered Oct 23 '22 17:10

Michael Burr


#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.

like image 39
immerhart Avatar answered Oct 23 '22 17:10

immerhart