Weird stuff with QVariant::toBool()
-
I have the following code:
QVariant temp{ workspace.value("Stacking/BackgroundCalibration", true) }; bool backgroundCalibration = temp.toBool(); // bool backgroundCalibration = workspace.value("Stacking/BackgroundCalibration", true).toBool();The third line is commented out as I wanted to inspect the variable in the debugger.
The debugger shows me this:

I suspect that the QVariant isn't being set correctly ... which is almost certainly my fault! Is that a correct representation of a bool QVariant or have I stuffed a character string in there when I shouldn't have?
In this case the settings were initialised from a file which contains lines like:
#V5WS#Stacking/BackgroundCalibration#false
The code that reads the file looks like this:WORKSPACESETTINGITERATOR it = findSetting(keyName); if (it != m_vSettings.end()) { // // Special Case for Stacking/Mosaic - don't allow Custom mode // if ("Stacking/Mosaic" == keyName && "2" == value) { value.setNum(static_cast<int>(SM_INTERSECTION)); // Force Intersection mode } // // In all cases when we enter here the variable "value" will be // a QString. // We need to convert it to the same type as is currently stored // QVariant variant(value); QMetaType type = it->value().metaType(); ZASSERT(variant.canConvert(type)); variant.convert(type); it->setValue(variant); bResult = true; }Thanks,
David -
I have the following code:
QVariant temp{ workspace.value("Stacking/BackgroundCalibration", true) }; bool backgroundCalibration = temp.toBool(); // bool backgroundCalibration = workspace.value("Stacking/BackgroundCalibration", true).toBool();The third line is commented out as I wanted to inspect the variable in the debugger.
The debugger shows me this:

I suspect that the QVariant isn't being set correctly ... which is almost certainly my fault! Is that a correct representation of a bool QVariant or have I stuffed a character string in there when I shouldn't have?
In this case the settings were initialised from a file which contains lines like:
#V5WS#Stacking/BackgroundCalibration#false
The code that reads the file looks like this:WORKSPACESETTINGITERATOR it = findSetting(keyName); if (it != m_vSettings.end()) { // // Special Case for Stacking/Mosaic - don't allow Custom mode // if ("Stacking/Mosaic" == keyName && "2" == value) { value.setNum(static_cast<int>(SM_INTERSECTION)); // Force Intersection mode } // // In all cases when we enter here the variable "value" will be // a QString. // We need to convert it to the same type as is currently stored // QVariant variant(value); QMetaType type = it->value().metaType(); ZASSERT(variant.canConvert(type)); variant.convert(type); it->setValue(variant); bResult = true; }Thanks,
David@Perdrix
What is your question?If you think you can rely on debugger view for some variable or memory location where you do not know what has been optimized where you cannot or should not.
(As side note, asking about the details of a debugger, or for that matter compiler, without saying what tool or platform won't get far.)
Especially since
temphere is just a local variable. If you want to see what is in it reliably do something like pass it as an argument to some function (which access it as a parameter) and look there, we are you sure it will have to be "valid".UPDATE:
On careful inspection of screenshot it looks like the value read in was a string,false, in the first place? It is the variablebackgroundCalibrationwhose value you do not like the look of? It would really help if you mentioned this in your post. Local int/bool variable is even more likely not to guarantee you will see its value get set in memory.But actually everything shown in the debugger, including
backgroundCalibration, was correct from the start. So don't know what the issue per your question ever was. -
P Perdrix has marked this topic as solved on
-
Problem found - the code that read from QSettings (elsewhere in the code) wasn't checking the MetaType of the existing QVariant() when reading all the values so bools were getting converted to QStrings.
-
@JonB said in Weird stuff with QVariant::toBool():
OK, wasn't worth the time looking and trying to send you an answer.
And you are still trying... :P