@Tom-asso said in Why does QQmlComponent::create() return null?:
Anyone know why?
QQmlComponent::errors()
Answer from raven-worx on forum.qt.ioQVariant - empty or Null
QWidget childAt(x, y) method always return NULL [Solved] | Qt Forum
[SOLVED]how to return an empty QString | Qt Forum
coding style - Is it good practice to have your C++/Qt functions always check all its arguments for null values? - Software Engineering Stack Exchange
You can return a null pointer if your function's return type is a pointer.
If the caller checks the return value and handles the case where the return value is null - then there shouldn't be any problem. if the caller tries to dereference this pointer, then problems do occur.
The fact that the function returns a pointer doesn't necessarily mean the developer needs to free the memory. the pointer could point to anything, including stack and global variables. The developer should look at the function's documentation to make sure what to do with the return value. Anyway, there is no problem in deleting null pointers, but again , one should look if she/he needs to deallocate something at the first place.
You can return a NULL pointer.
Do I have to free that pointer again?
delete is only necessary if you allocate space on the free store.
However, if you use C++11 or higher, I recommend the usage of nullptr instead of NULL. In this particular code, it doesn't provide a significant improvement but I consider it good style, also to draw the line between C and C++.
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.
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
QT's editing widgets use QString.
QString has many methods that you can use.
Use isEmpty or isNull methods of QString.
I hope this helps.
After a bit of playing, I think this will work for you
if (flightNumber.text.trim().length == 0) {
flightNumbertext.text.color = 'red';
}
The function trim() is like QString::trimmed(). It removes both leading and trailing spaces from the string.
The documentation of QSqlQuery says to use QVariant(QMetaType::QString) to bind NULL to a column value. However, the current version of qvariant.h available on Ubuntu deletes that constructor on line 556, with a comment claiming the use of that style of constructor is flawed in the first place - as such it will fail to compile if you follow the documentation's advice.
What's the correct way to do this? I'm happy to be told I'm an idiot if I've missed something obvious!