Sur cette page

Avertissement concernant l'utilisation d'une affectation dans une condition

Cette catégorie d'avertissement est orthographiée « [assignment-in-condition] » par qmllint.

Avertissement concernant l'utilisation d'une affectation dans une condition

Que s’est-il passé ?

Vous avez utilisé une affectation à l’intérieur d’une condition « if-condition ».

Pourquoi est-ce un problème ?

Il s'agit souvent d'une erreur ; une comparaison aurait dû être utilisée. Même si c'était intentionnel, cela est souvent considéré comme source de confusion.

Exemple

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

Pour corriger cet avertissement, remplacez l'affectation par une comparaison s'il s'agissait d'une erreur. Sinon, placez l'affectation entre parenthèses pour indiquer qu'elle a été effectuée intentionnellement.

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.