QJSEngine全局字符串比较

QJSEngine global string compare

本文关键字:比较 字符串 全局 QJSEngine      更新时间:2023-10-16

我使用Qt 5.11.2,在我的应用程序中我使用QJSEngine,在示例中我有一个脚本:

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("-----------");
}

我已经将此脚本连接到应用程序中的一个按钮,当我单击该按钮时,脚本会调用connect()函数。我已经注册了一些用于脚本的全局:

strFirstScan = "true"
a = 123
b = "Hello"
c = {"a":1,"b":"A","c":{"aa":1}}

单击按钮时脚本应用程序的输出为:

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] -----------

我从来没有看到"First Scan",变量的类型仍然是字符串,因为它没有到达eval语句。

为什么比较不起作用?我已经尝试了很多替代方案:

if ( strFirstScan == "true" ) {

if ( strFirstScan.compare("true") == 0 ) {

这些都不好,为什么比较不起作用?

[编辑]我已将脚本修改为:

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("-----------");
}

使用这样的脚本,我在输出中得到以下内容:

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] -----------

但是,如果我在if条件中添加任何比较,将字符串与"true"if进行比较,则不会传递到第一个扫描条件中。

[Edit2]我将创建全局变量"strFirstScan"的代码修改为:

pobjScriptEng->globalObject().setProperty("strFirstScan", QJSValue("true"));

这现在解决了问题和我的脚本:

if ( strFirstScan == "true" ) {

工作。

根据程序输出判断,strFirstScan不是true,而是"true"

您可以通过在日志输出中的值周围添加某种引号来验证这一点。即:console.info("strFirstScan: '" + strFirstScan + "', typeof: " + typeof strFirstScan);

以下是演示您的问题的片段:

var yourCase = '"true"';
console.info('value: ' + yourCase + ', type: ' + typeof yourCase + ', len: ' + yourCase.length);
console.info(yourCase === "true");
var youWant = 'true';
console.info('value: ' + youWant + ', type: ' + typeof youWant + ', len: ' + youWant.length);
console.info(youWant === "true");

然而,从您提供的内容中无法判断您的代码为什么会如此运行。

我将创建全局变量"strFirstScan"的代码修改为:

pobjScriptEng->globalObject().setProperty("strFirstScan", QJSValue("true"));

这现在解决了问题和我的脚本:

if ( strFirstScan == "true" ) {

这解决了问题,现在可以工作了。