【QML】警告Name is declared more than once

1. 问题:

qml函数中的不同块中定义同名变量,报警:Name is declared more than once

举例:

c 复制代码
function test(a){
	if(a === "1"){
		var re = 1;
		console.log(re);
	}else{
		var re = 2;	//这里会报警:Name is declared more than once
		console.log(re);
	}
}

2. 原因:

  • qml与C++不同。
  • qml在函数中声明的变量在函数中的任何地方都是可见的,即使在嵌套块或语句条件中声明也是如此。重新声明变量没有效果。

参考:

Qt:QML:JavaScript 和QML 语法错误表

相关推荐
IOT-Power3 小时前
QT 事件驱动架构
开发语言·qt·架构
umeelove355 小时前
使用 Qt 插件和 SQLCipher 实现 SQLite 数据库加密与解密
数据库·qt·sqlite
娇娇yyyyyy6 小时前
QT编程(12): QDragEvent事件
qt·microsoft
fareast_mzh6 小时前
[MySQL] Package ‘libtirpc‘, required by ‘virtual:world‘, not found
数据库·qt·mysql
IOT-Power6 小时前
Qt+C++ 控制软件架构实例
开发语言·c++·qt
Ronin3057 小时前
【Qt常用控件】输入类控件
开发语言·qt·常用控件·输入类控件
IOT-Power7 小时前
工业级大型 Qt / C++ 项目中 EventBus 的实现方式
qt
娇娇yyyyyy19 小时前
QT编程(10): QLineEdit
开发语言·qt
墨月白21 小时前
[QT]浮点数转换成4个字节的十六进制(IEEE 754标准)
qt·ieee
小温冲冲1 天前
QML vs Qt Widgets:深度对比与选型实战指南
开发语言·c++·qt