Documentation on isDetached?
-
I can see in Qt Creator that the context help shows isDetached on a QVariant type, I cannot find any documentation on this function, I just want to confirm if it means what I think.
I'm using Qt 4.8 on this project. Does isDetached mean that if true the QVariant isn't set-up ?
I'm calling property(cpszArray) and I know this property doesn't exist so the QVariant returned does return true when isDetached is called.
[Edit] In my test code:
clsArray& clsJSON::operator[](const char* cpszArray) { QVariant varArray(property(cpszArray)); bool blnIsDetached(varArray.isDetached()); clsArray objArray; if ( blnIsDetached == true ) { setProperty(cpszArray, objArray.toList()); varArray = proprty(cpszArray); blnIsDetached = varArray.isDetached(); } return objArray; }The above code is very much a work in progress and I know there are issues, the main reason for posting it is to highlight what I'm seeing, when property is called the variant varArray.isDetached() returns true, I was assuming because the property didn't exist, then after calling setProperty I test it again, but it still returns true. The function toList:
tVarList toList() { return mvlstElements; }tVarList:
typedef QList<QVariant> tVarList;[Edit 2], still no idea what isDetached means, but I've modified the function and now it makes sense:
clsArray& clsJSON::operator[](const char* cpszArray) { QVariant varArray(property(cpszArray)); bool blnIsValid(varArray.isValid()); clsArray objArray; if ( blnIsValid != true ) { setProperty(cpszArray, objArray.toList()); varArray = proprty(cpszArray); blnIsValid = varArray.isValid(); } return objArray; }On the initial call isValid returns false, after calling setProperty then second call to isValid returns true.
-
I can see in Qt Creator that the context help shows isDetached on a QVariant type, I cannot find any documentation on this function, I just want to confirm if it means what I think.
I'm using Qt 4.8 on this project. Does isDetached mean that if true the QVariant isn't set-up ?
I'm calling property(cpszArray) and I know this property doesn't exist so the QVariant returned does return true when isDetached is called.
[Edit] In my test code:
clsArray& clsJSON::operator[](const char* cpszArray) { QVariant varArray(property(cpszArray)); bool blnIsDetached(varArray.isDetached()); clsArray objArray; if ( blnIsDetached == true ) { setProperty(cpszArray, objArray.toList()); varArray = proprty(cpszArray); blnIsDetached = varArray.isDetached(); } return objArray; }The above code is very much a work in progress and I know there are issues, the main reason for posting it is to highlight what I'm seeing, when property is called the variant varArray.isDetached() returns true, I was assuming because the property didn't exist, then after calling setProperty I test it again, but it still returns true. The function toList:
tVarList toList() { return mvlstElements; }tVarList:
typedef QList<QVariant> tVarList;[Edit 2], still no idea what isDetached means, but I've modified the function and now it makes sense:
clsArray& clsJSON::operator[](const char* cpszArray) { QVariant varArray(property(cpszArray)); bool blnIsValid(varArray.isValid()); clsArray objArray; if ( blnIsValid != true ) { setProperty(cpszArray, objArray.toList()); varArray = proprty(cpszArray); blnIsValid = varArray.isValid(); } return objArray; }On the initial call isValid returns false, after calling setProperty then second call to isValid returns true.
@SPlatten said in Documentation on isDetached?:
still no idea what isDetached means, but I've modified the function and now it makes sense:
I may be wrong, but AFAIK, Qt classes are designed to avoid data copies and isDetached() is to be aware if this class instance (for example
QString) holds its own data copy or points to data of another instance.
You can force detaching by calleddetach()EDIT: for more details take a look at https://doc.qt.io/qt-5/implicit-sharing.html
-
@KroMignon said in Documentation on isDetached?:
I may be wrong, but AFAIK, Qt classes are designed to avoid data copies and isDetached() is to be aware if this class instance (for example QString) holds its own data copy or points to data of another instance.
Jus to confirm that this is the case. If you look up isDetached() in qvariant.h for Qt 4.8, it's defined as
inline bool QVariant::isDetached() const { return !d.is_shared || d.data.shared->ref == 1; }