このページでは

条件式での代入の使用について警告する

この警告カテゴリは、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.