I am using Qt 5.11.2, in my application I am using QJSEngine, in my example I have a script:
function connect() {
console.info("-----------");
if ( strFirstScan.localeCompare("true") == 0 ) {
console.info("First scan");
strFirstScan = "false";
a = eval(a);
b = eval(b);
c = eval(c);
}
console.info("strFirstScan: " + strFirstScan + ", typeof: " + typeof strFirstScan);
console.info("a: " + a + ", typeof: " + typeof a);
a++;
console.info("b: " + b + ", typeof: " + typeof b);
console.info("c: " + c + ", typeof: " + typeof c);
console.info("-----------");
}
I have connected this script to a button in the application, when I click the button the connect() function is called by the script. I have registered some globals for use with the script:
strFirstScan = "true"
a = 123
b = "Hello"
c = {"a":1,"b":"A","c":{"aa":1}}
The output from the script application when the button is clicked is:
2018-12-28 09:55:10.079663+0000 XMLMPAM[2470:247691] [js] -----------
2018-12-28 09:55:10.079718+0000 XMLMPAM[2470:247691] [js] strFirstScan: "true", typeof: string
2018-12-28 09:55:10.079742+0000 XMLMPAM[2470:247691] [js] a: 123, typeof: string
2018-12-28 09:55:10.079775+0000 XMLMPAM[2470:247691] [js] b: 'hello', typeof: string
2018-12-28 09:55:10.079804+0000 XMLMPAM[2470:247691] [js] c: {"a":1,"b":"A","c":{"aa":1}}, typeof: string
2018-12-28 09:55:10.079832+0000 XMLMPAM[2470:247691] [js] -----------
I never see "First Scan" and the types of the variables remains string as it isn't getting to the eval statements.
Why isn't the compare working? I've tried a number of alternatives:
if ( strFirstScan == "true" ) {
and
if ( strFirstScan.compare("true") == 0 ) {
None of these are any better, why isn't the compare working?
[Edit] I've modified the script to:
function connect() {
console.info("-----------");
if ( typeof strFirstScan == "string" ) {
console.info("First scan");
console.info("strFirstScan: " + strFirstScan + ", typeof: " + typeof strFirstScan);
strFirstScan = 0;
a = eval(a);
b = eval(b);
c = eval(c);
}
console.info("a: " + a + ", typeof: " + typeof a);
a++;
console.info("b: " + b + ", typeof: " + typeof b);
console.info("c: " + c + ", typeof: " + typeof c);
console.info("-----------");
}
With the script like this I get the following in the output:
2018-12-28 10:22:31.267553+0000 XMLMPAM[2993:335615] [js] -----------
2018-12-28 10:22:31.267595+0000 XMLMPAM[2993:335615] [js] First scan
2018-12-28 10:22:31.267629+0000 XMLMPAM[2993:335615] [js] strFirstScan: "true", typeof: string
2018-12-28 10:22:31.267804+0000 XMLMPAM[2993:335615] [js] a: 123, typeof: number
2018-12-28 10:22:31.267832+0000 XMLMPAM[2993:335615] [js] b: hello, typeof: string
2018-12-28 10:22:31.267877+0000 XMLMPAM[2993:335615] [js] c: [object Object], typeof: object
2018-12-28 10:22:31.267897+0000 XMLMPAM[2993:335615] [js] -----------
However if I add any comparison to the if condition comparing the string against "true" if does not pass into the first scan condition.
[Edit2] I modified my code that creates the global variable 'strFirstScan' to:
pobjScriptEng->globalObject().setProperty("strFirstScan", QJSValue("true"));
This now resolves the problem and my script:
if ( strFirstScan == "true" ) {
Works.
I modified my code that creates the global variable 'strFirstScan' to:
pobjScriptEng->globalObject().setProperty("strFirstScan", QJSValue("true"));
This now resolves the problem and my script:
if ( strFirstScan == "true" ) {
This fixes the problem and it now works.