0

I had some problem with validating values in my QtScript script. The validator function looked like this:

function isValueInvalid(value) {
    return typeof value=="undefined" || value == null || value == "" || value == "X";
}

The string "X" is also invalid value in my setting. The values are added to some object like this:

someQtscriptValue.setProperty(name, myQtScriptEngine.newVariant(someQVariant));

Now the thing is that values which convert to "undefined" as string were constantly passing the typeof check. So I added test debug output. I printed the whole someQtscriptValue in the script:

// This is qtscript code:
print("Validating row "+JSON.stringify(someQtscriptValue));

And the result is confusing (shortened, it looked all the same):

Validating row {"SJZ_A":{},"SJZ_B":{},"SJZ_C":{},"SJZ_D":{},"SJZ_E":{}}

I also decided to print some info about each value:

var propName = "name of one of the properties";
var isValid = !isValueInvalidCEPS(someQtscriptValue[propName]);
print("someQtscriptValue[\""+propName+"\"] = "+someQtscriptValue[propName]+"  ("+(isValid?"valid":"invalid")+") type="+(typeof someQtscriptValue[propName]));

And this is even more surprising:

someQtscriptValue["name1"] = ULSE  (valid) type=object
someQtscriptValue["name2"] = 22  (valid) type=object
someQtscriptValue["name3"] = undefined  (valid) type=object

So apparently, if I make QScriptValue from QVariant it behaves like an object, since all values including the "undefined" one had type as object.

So what is the correct way to create QScript values from QVariants?

Tomáš Zato
  • 50,171
  • 52
  • 268
  • 778

1 Answers1

1

So, as far as QVariants are concerned, this method works just fine:

QScriptEngine::toScriptValue

QVariant can be passed to it. If the QVariant is invalid, it will end up as an undefined QTscript value. Numeric qvariants turn as numbers and strings as strings.

Tomáš Zato
  • 50,171
  • 52
  • 268
  • 778