@Tom-asso said in Why does QQmlComponent::create() return null?:

Anyone know why?

QQmlComponent::errors()

Answer from raven-worx on forum.qt.io
🌐
Qt Forum
forum.qt.io › home › qt development › qml and qt quick › how do i return "null" from c++ to qml?
How do I return "null" from C++ to QML? | Qt Forum
August 18, 2019 - eddy Not exactly the same thing. That link talks about C++ producing a nullptr value when the expected type is pointer-to-something-derived-from-QObject, but I'm trying to return a QVariant, not a pointer to a QVariant.
Discussions

QVariant - empty or Null
No other value will return an empty QVariant so it should be distinct. Otherwise you would have to invent some unique QVariant for NULL. However, I think the Qt database routines want you to indicate the type of the "NULL", to match the database column type. You want your nulls to match what ... More on forum.qt.io
🌐 forum.qt.io
2
0
April 27, 2023
QWidget childAt(x, y) method always return NULL [Solved] | Qt Forum
Hi all, I'm rather new to Qt and have been playing around with it to develop a C++ desktop application for a school project. I must say I'm quite surprised w... More on forum.qt.io
🌐 forum.qt.io
December 24, 2011
[SOLVED]how to return an empty QString | Qt Forum
Depending on circumstances/optimizer/... 4 could produce the same code, but I wouldn't rely on that. 2 runs through several out-of-line functions and is uniformly worse, it will not even compile if e.g. QT_NO_CAST_FROM_ASCII is used. Note that returning QString::null does not prevent the creation ... More on forum.qt.io
🌐 forum.qt.io
November 3, 2014
coding style - Is it good practice to have your C++/Qt functions always check all its arguments for null values? - Software Engineering Stack Exchange
Should I be introducing a runtime error, halting the program, instead of simply returning the function? ... Are there any performance considerations, to the extent that checking for null values may take longer depending on the type? Beyond c++ and Qt, what other languages benefit from this style? More on softwareengineering.stackexchange.com
🌐 softwareengineering.stackexchange.com
January 12, 2016
🌐
Runebook
runebook.dev › english › articles › qt
Troubleshooting Null QString Issues in Qt Applications
Most Qt functions that deal with strings will return empty QString objects to indicate the absence of a value, not null QString objects.
🌐
Qt Forum
forum.qt.io › home › qt development › general and desktop › qwidget childat(x, y) method always return null [solved]
QWidget childAt(x, y) method always return NULL [Solved] | Qt Forum
December 24, 2011 - When I ask for the amount of children the central widget has by calling @children()->size()@ this seems to be correct. However when I try to retrieve a child, a Pixel instance, by calling the @childAt(x, y)@ method this always returns 0, no matter what values I pass as arguments.
🌐
Qt Forum
forum.qt.io › home › qt development › general and desktop › [solved]how to return an empty qstring
[SOLVED]how to return an empty QString | Qt Forum
November 3, 2014 - To return an empty string, you can do those: @ return QString(); // 1 return ""; // 2 return QString::null; // 3
Top answer
1 of 3
11

Kind of echoing Basile here but with a slightly more negative tone. I'd say "yes" too, but be careful with that if you are allowing silent violations of preconditions. As a personal example, I worked in a C codebase that did this kind of stuff through an API:

int f(struct Foo* foo)
{
    if (!foo)
        return error;
    ...
    return success;
}

Perhaps even with a debug log in the middle. Yet it was a massive codebase, tens of millions of lines of code, over 30 years of development, and with pretty shoddy standards. The warnings in places like logs were filled to the brim with developers having developed the habit of ignoring the output.

I found, most unfortunately, when trying to change such error checks into obnoxiously-loud assertions that there were thousands of places in the system that were violating these preconditions and simply ignoring the errors (if there were any reported, some functions just returned without doing anything in those cases). The developers just worked around it and kept trying stuff and adding more code until their code "worked", blissfully unaware of their original mistake that they were passing nulls to a function that did not allow it.

As a result, I strongly recommend doing like this:

void MyClass::setMyString(QString input)
{
    assert(!input.isNull() && "Error: setMyString() received NULL argument.");
    m_MyString = input;
    emit myStringChanged();
}

This will bring your application to a grinding halt if the assertion fails, showing you the precise source file and line number in which the assertion failed along with this message.

This is for cases where your function is not allowed to receive null (in your case, "null" here means "empty string"). For ones that are allowed, you might simply do something else, but it wouldn't be reporting a bug (for that, use the loud and obnoxious assert which cannot be ignored).

An example of where you might use if might be like this:

bool do_something(int* out_count = 0)
{
    ...
    if (out_count)
       *out_count = ...; // write the result back.
    return true;
}

For ones that are not allowed to receive a null, use references when possible. However, you might still encounter "nulls" if you accept smart pointers, e.g., or if you expand your definition of "null" to include empty strings as another example. In those cases, assert your preconditions liberally if you can. assert has the advantage of not only bringing your application to a grinding halt (good thing), but also pointing out where it was brought to a grinding halt even outside of a debugging session (but still with a debug build, e.g.), and it's also an effective means of documentation.

qDebug is a little too silent usually. A team with good habits that never allows those outputs to be ignored might get away with keeping those to zero. But an assert will protect you even against the sloppiest teams by bringing the entire process to a grinding halt, making these programmer errors impossible to ignore. Another minor benefit of assert is that it can't slow down your release (production) builds, while if (...) qDebug(...) will.

For errors caused by external sources outside of your control (ex: failing to connect to a server that was down, failing to allocate a huge block of memory, trying to read from a file the user tried to load that was corrupt, etc), throw exceptions, but not for programmer bugs like accessing an array out of bounds or receiving a null in a place that never should receive null (unless you're working in a very mission-critical software where the software attempts to gracefully recover from even programmer bugs as opposed to making them as easy to detect and reproduce as possible). For these cases, assert.

2 of 3
4

As I commented, yes you should usually check against nullptr and always document if a public function accepts a null pointer or not (that it, document a function as accepting a null pointer, or as requiring a non-null valid pointer).

Checking against a nullptr is -on most current C++11 implementations- very quick: dereferencing a pointer might make a cache miss (always slow), but comparing it against 0 is a fast arithmetic operation (nearly free).

Use assert if a pointer should always be null and valid.

Use an if to handle the nullptr case if it is genuinely possible at runtime.

Use C++ references to convey that the address is always valid. That is, if you have void foo(int*) and you know that the address is never null and always valid, code a void foo(int&) function instead.

Compilers are permitted to optimize the null pointer test after you have dereferenced it:

 void foo(struct bar_st*p) {
   int x = p->somefield;
   if (!p) return; /// test can be removed by optimization
Find elsewhere
🌐
Runebook.dev
runebook.dev › en › docs › qt › qstring › isNull
qt - A Developer's Guide to QString Null vs. Empty States
September 18, 2025 - If the user wasn't found in the database at all, the function might return a null string (QString()) to signal an error or a non-existent record.
🌐
Qt Forum
forum.qt.io › home › qt development › general and desktop › [solved] qjsonobject value returns null
[SOLVED] QJsonobject value returns NULL | Qt Forum
September 15, 2014 - Interested in AI ? www.idiap.ch Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct ... Yes. For me too parse error coming as no error occured and jsondocument.object returns the value.
🌐
Qt Project
lists.qt-project.org › pipermail › interest › 2016-May › 022609.html
[Interest] Return null QSGNode from updatePaintNode to erase previously drawn things
May 11, 2016 - At some point I want to erase all ... could by returning a null qsgnode from updatePaintNode, but the stuff remains visible. I have a few ideas how to work around this, but I just wonder if this is considered normal behavior. > _______________________________________________ > Interest mailing list > Interest at qt-project.org ...
🌐
Qt Forum
forum.qt.io › home › qt development › general and desktop › qtextstream::readline() returns null
QTextStream::readLine() returns null | Qt Forum
February 10, 2025 - qDebug() << "\nType in your search phrase:\n"; QTextStream s(stdin); const auto phrase = s.readLine(); qDebug() << " you entered" << phrase; return a.exec();
🌐
Qt Forum
forum.qt.io › home › qt development › general and desktop › inexplicable qsqlquerymodel handling of null value
Inexplicable QSqlQueryModel handling of NULL value | Qt Forum
May 2, 2018 - I think the base class's returned "null" QVariant gets turned into an "invalid" one, and then an invalid QVariant gets treated as 0 for an int or '' for a string, losing the original NULL completely, and leading to the behaviour I saw. By wrapping the call to the base class method in a sip.enableautoconversion(QtCore.QVariant, False), I am telling PyQt not to do any conversion, and hence I restore the desired behaviour.
🌐
Qt Forum
forum.qt.io › home › qt development › general and desktop › windowhandle() will return null;
windowHandle() will return NULL; | Qt Forum
August 27, 2016 - In manual ,QWidget::windowHandle() returns a valid instance of a QWindow only after the widget has been shown. It is therefore recommended to delay the initi...