I made a function which returns a QString. At some points in my function it should return an empty QString.
Just returning "" doesn't work. When I use QString::isEmpty() it's not.
My "emergency plan" was to return an "empty" string and check with it whether the text is "empty". But I don't think that's good style.
So how do I return an empty QString?
The idiomatic way to create an empty QString is using its default constructor, i.e. QString(). QString() creates a string for which both isEmpty() and isNull() return true.
A QString created using the literal "" is empty (isEmpty() returns true) but not null (isNull() returns false).
Both have a size()/length() of 0.
According to the QString docs, returning "" should work; as should returning QString().
Try printing out the string you are testing to make sure it's really empty:
printf("xxx [%s]\n", myStr.toUtf8().constData());
Related
I have an JSON object like this:
{ "foo": null }
How do I check if the value of foo is a literal null. I found the function JsonObject::isNull() but that apparently is for
testing whether the JsonObject points to an object or not
That is not what I want my code to check, but I couldn't find a solution to this problem.
According to the doc: https://arduinojson.org/v6/issues/cannot-use-null/
If c++11 is available (which has nullptr), you can check if the json object's value is null or not like so:
if (!doc["foo"]) {}
or
if (doc["foo"] == nullptr) {}
If c++11 is not available, you can use the isNull() method:
if (doc["foo"].isNull()) {}
I am working on a module in which GUI and back-end interact by message passing.
I initialised a char* as nullptr. GUI takes a QStringList which is tokenized. I cannot do the changes in the tokenized list. I receive a "(null)" in the list. Now I feel like by comparing to "(null)" is not the way to go. Is there any initialisation for char* which would be taken as an empty string by QString?
Use an empty string when constructing a QString when the pointer is NULL.
QString obj = (ptr == nullptr? QString("") : QString(ptr));
And then use obj.
Not sure to understand what you want but if i am not mistaken :
char *c = nullptr;
QString str(c);
str.isEmpty(); // return true
I am using QT 5.7 and MVSC - 2010 (yes, it is old, but I have to due to project)
I have a template function
template<T>
T foo (QString qs)
{
return qvariant_cast<T>(qs);
}
If I make
void main()
{
std::string str = "42"
QString qss = QString::fromStdString(str);
std::string another_str = foo<std::string>(qss );
}
Then another_str will be "";
While toStdString method works perfectly. What is the problem of qvariant_cast?
P.S. I have declared qt meta type, so qvariant_cast is compling, but returning empty string.
According to the manual call qss.canConvert<std::string>() to find out whether a type can be converted. If the value cannot be converted, a default-constructed value will be returned. It is your case, the empty std::string is returned.
Hello everyone I am trying to get to know pointers better and I stumbled into a Qt type change. I have made a QString array and gave the pointer to the array to a method. But when I try to use a QString functions it give a error and says that it is a QCharRef which does not have the member function isEmpty().
The code:
QString data_array[2][3] =
{
{"11:28:8","Room 1","Presence detected"},
{"11:38:8","Room 1","No presence"}
}
bool method(QString *_data_array)
{
QString *data_array = _data_array;
return data_array[0][1].isEmpty(); /* changed to QCharRef */
}
My question is why does this happen and how can I prevent it or change it?
The reason for which you are getting QCharRef is due to how QString is built. The [] operator returns one character from a QString (QString is built up from QChars, much like strings in C/C++ are character arrays). From the Qt documentation:
The return value is of type QCharRef, a helper class for QString. When you get an object of type QCharRef, you can use it as if it were a QChar &. If you assign to it, the assignment will apply to the character in the QString from which you got the reference.
So what that means for you is that when you use the lovely square bracket operators, you are no longer using a QString, you are using a QChar reference.
As for how to change it, QChar's isNull() seems like it would fit your uses. so instead try return data_array[0][1].isNull(); and that should work.
I would also look into QStringList if you're doing things with lists of strings
I have created a Settings class wrapper around QSettings that provides the following methods for access in QML:
Q_INVOKABLE void setValue(Key key, const QVariant &newValue);
Q_INVOKABLE QVariant value(Key key);
In the user interface, there are CheckBox instances that look like this:
CheckBox {
checked: Settings.value(Settings.KeySomeBoolSettings)
}
QSettings loads boolean values into QVariant as a QString type as "true" or "false" (which is fine). The problem is when QML converts this implicitly to a boolean expression as needed by the checked property, it is always converting to true (even if it is actually "false"). I've found a workaround to detect whenever a QVariant in Settings::value() is actually a boolean.
QVariant Settings::value(Key key)
{
QVariant value = mSettings->value(stringNameForKey(key), mDefaultValues[key]);
//this block is the workaround
if (QString(value.typeName()) == "QString" &&
(value.toString() == "false" || value.toString() == "true"))
return QVariant(value.toBool());
return value;
}
When detected, the actual QVariant returned is QVariant(value.toBool()) which causes the internal type to actually be bool. So when the internal type is bool, then QML can make the implicit conversion fine. From what I can tell, QML is taking the varient literally as a string, and then converting that to a bool which is always true unless the string is blank. Is this a bug, or am I doing something wrong?
As described in ECMAScript language specification, converting to boolean return false, if value is +0, -0, null, false, NaN, undefined, or the empty string. All other values, including any object or the string "false", converted to true.
Also, in QML you can use Qt Labs Settings QML Type instead of wrapper around QSettings