QJSEngine 全局字符串比较
QJSEngine global string compare
我正在使用 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 ) {
None 其中更好,为什么比较不起作用?
[编辑] 我已将脚本修改为:
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" 进行比较,如果没有进入第一个扫描条件。
[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" ) {
这解决了问题,现在可以使用了。
我正在使用 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 ) {
None 其中更好,为什么比较不起作用?
[编辑] 我已将脚本修改为:
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" 进行比较,如果没有进入第一个扫描条件。
[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" ) {
这解决了问题,现在可以使用了。