对条件语句中使用赋值操作发出警告
qmllint 将此警告类别拼写为[assignment-in-condition] 。
对在条件中使用赋值发出警告
发生了什么?
您在if-condition 中使用了赋值语句。
为什么这样不好?
这通常是一种错误,本应使用比较运算符。即使是故意的,也往往会被认为容易引起混淆。
示例
import QtQuick
Item {
id: root
Component.onCompleted: {
// mistake: should have been a comparison
if (width = height)
console.log("A square")
let mypoint = Qt.point(1,2)
let hit = false
// intentional, but possibly misleading
if (hit = root.contains(myPoint))
console.log("hit")
root.enabled = hit
}
}要解决此警告,如果这是个错误,请将赋值操作改为比较运算。否则,请将赋值操作用圆括号括起来,以表明这是有意为之。
import QtQuick
Item {
id: root
Component.onCompleted: {
// fixed
if (width === height)
console.log("A square")
let mypoint = Qt.point(1,2)
let hit = false
// intentional
if ((hit = root.contains(point)))
console.log("hit")
root.enabled = hit
}
}© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.