동등성 비교 시 유형 강제 변환
qmllint에서는 이 경고 범주를 ‘ [equality-type-coercion] ’로 표기합니다.
느슨한 등가 비교 시의 형 변환
무슨 일이 있었나요?
두 값이 느슨한 비교 연산자를 사용하여 동일성 비교가 수행되었습니다.
왜 문제가 될까요?
느슨한 비교 연산자는 동일성 확인 전에 값을 다른 유형으로 강제 변환할 수 있습니다. 이로 인해 예상치 못한 결과가 발생할 수 있습니다.
예시
다음은 Rectangle 목록과 ` TextInput`를 사용하는 예제입니다. 사용자가 숫자를 입력하면 해당 인덱스에 있는 Rectangle이 강조 표시됩니다. 이 코드에는 결함이 있습니다. 입력이 비어 있을 경우, "" == 0이므로 첫 번째 Rectangle이 빨간색으로 강조 표시됩니다.
import QtQuick
Item {
TextInput {
id: input
}
Repeater {
model: 3
Rectangle {
// first rectangle is red on empty input
color: input.text == index ? "red" : "blue"
}
}
}일반적으로 엄격한 비교 연산자인 === 및 !== 를 사용하십시오. 형 변환이 발생한다는 사실을 알고 있더라도, 대신 명시적인 형 변환과 엄격한 비교를 사용하는 것이 좋습니다.
© 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.