23

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?

skaffman
  • 398,947
  • 96
  • 818
  • 769
user1065969
  • 589
  • 4
  • 10
  • 19

2 Answers2

54

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().

Michael Burr
  • 333,147
  • 50
  • 533
  • 760
  • Michael thanks for your response, but one thing is unclear to me - does str.clear() leaves empty string or nullified one. If empty is there a way to nullify it? – yatsek Mar 30 '12 at 13:31
  • `QString::clear()` sets the string to null (i.e., after `str.clear()`, str.isNull() will return `true`). – Michael Burr Mar 30 '12 at 15:06
15
#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.

  • 2
    Thumbs up for explicitly including QString::null. I like the way it eliminates any/all ambiguity in intention. – rob Sep 24 '15 at 20:59
  • 1
    @RobertOliveira Agreed! Had no idea about this until just now. – Andrew Apr 24 '16 at 14:26
  • @rob One downside of using `QString::null` is that, on Windows, it will cause a data symbol to be imported - which in turn prohibits [delay-loading](https://learn.microsoft.com/en-us/cpp/build/reference/delayload-delay-load-import?view=vs-2019) the Qt (resp. Qt Core) library. – Frerich Raabe Jul 02 '19 at 11:02
  • 4
    Qt Creator now announces that `QString::null` is deprecated. It recommends the usage of `String()` instead. – Ronan Paixão Mar 20 '21 at 03:26
  • @RonanPaixão could you give us a short example? I'm struggling with that as well. – Gwyneth Llewelyn Aug 14 '22 at 17:19
  • 1
    @GwynethLlewelyn see the accepted answer for details. In essence, you just use the default initializer. Simply this should work: `QString phoneNumber;`. – Ronan Paixão Aug 16 '22 at 16:51
  • @RonanPaixão ah, I get you! I see; but my problem is actually that I'm refactoring old code which has some default values on a class method — one of which being a `QString`! I wonder, how should I deal with that? (my workaround: for the moment, ignore the default value and cross my fingers and hope it'll work... somehow!) – Gwyneth Llewelyn Aug 20 '22 at 16:56