이 페이지에서

컨텍스트 속성

qmllint에서는 이 경고 범주를 ' [context-properties] '로 표기합니다.

잠재적인 컨텍스트 속성 접근이 감지되었습니다

무슨 일이 있었나요?

잠재적인 컨텍스트 속성 액세스가 감지되었습니다.

이것이 왜 문제인가요?

QML 코드에서 컨텍스트 속성을 사용하면, 코드를 작성할 때 염두에 둔 특정 컨텍스트에 대한 의존성이 생성됩니다. 이는 코드가 사용될 다른 곳에서 컨텍스트가 다를 수 있으므로 코드의 재사용성을 제한합니다. 게다가 이 의존성은 선언되지 않았습니다. 컨텍스트를 임포트하거나 예상되는 조건을 명시적으로 표기하지 않았기 때문입니다. 따라서 코드를 재사용하려는 사람은 재사용되는 위치에 해당 코드를 실행하는 데 충분한 컨텍스트가 있는지 파악하기 어려울 것입니다.

QML 툴링은 context 속성을 사용할 수 없습니다. 컴파일러는 이 파일을 C++로 컴파일할 수 없으며, qmllint 역시 QML Language Server 해당 파일에 대해 유용한 경고를 제공할 수 없습니다.

예시

// main.cpp
int main(int argc, char **argv)
{
    QGuiApplication app(argc, argv);
    QQuickView view;
    view.rootContext()
        ->setContextProperty("myProperty", QDateTime::currentDateTime());
    view.loadFromModule("MyModule", "Main");
    view.show();
    app.exec();
}
// Main.qml
import QtQuick

Item {
    Component.onCompleted: console.log(myProperty)
}

이 경고를 해결하려면 다음을 수행할 수 있습니다:

  • 속성을 필수로 지정합니다.
  • 싱글톤을 사용합니다.
  • .contextProperties.ini 에 항목을 추가합니다.

필수 속성 사용

구성 요소마다 값이 다른 컨텍스트 속성의 경우 필수 속성을 사용하십시오. 필수 속성을 사용하여 이 경고를 해결하려면 setContextProperty 대신 setInitialProperties 메서드 중 하나를 사용하십시오:

또한 Main.qml 에서 필요한 속성을 정의하십시오.

// main.cpp
int main(int argc, char **argv)
{
    QGuiApplication app(argc, argv);
    QQuickView view;
    view.setInitialProperties( { {"myProperty", QDateTime::currentDateTime()}  } );
    view.loadFromModule("MyModule", "Main");
    view.show();
    app.exec();
}
// Main.qml
import QtQuick

Item {
    required property date myProperty
    Component.onCompleted: console.log(myProperty)
}

“C++에서 QML로 상태 노출하기”도 참조하십시오.

싱글톤 사용

모든 컴포넌트에서 동일한 값을 갖는 컨텍스트 속성에는 싱글턴을 사용하십시오. 싱글턴을 사용하여 이 경고를 해결하려면 setContextProperty 호출을 제거하고, 싱글턴을 정의한 다음, 컨텍스트 속성 사용 부분을 싱글턴 속성으로 대체하십시오.

// mysingleton.h
class MySingleton : public QObject {
    Q_OBJECT
    QML_SINGLETON
    QML_ELEMENT
    Q_PROPERTY(QDateTime myProperty MEMBER m_myProperty NOTIFY myPropertyChanged FINAL)

    QDateTime m_myProperty = QDateTime::currentDateTime();
signals:
    void myPropertyChanged();
};
// main.cpp
int main(int argc, char **argv)
{
    QGuiApplication app(argc, argv);
    QQuickView view;
    view.loadFromModule("MyModule", "Main");
    view.show();
    app.exec();
}
// Main.qml
import QtQuick

Item {
    Component.onCompleted: console.log(MySingleton.myProperty)
}

'C++에서 QML로 상태 노출'도 참조하십시오.

.contextProperties.ini에 항목 추가

컨텍스트 속성을 대체할 수 없는 경우, 프로젝트 소스 디렉터리에 .contextProperties.ini 파일이 없다면 새로 생성하고 다음 내용을 작성하십시오:

[General]
disableUnqualifiedAccess = "myProperty"
warnOnUsage = "myProperty"
disableHeuristic = false

.contextProperties.ini 파일이 이미 존재하는 경우 이 경고를 무시하려면, 기존 목록 끝에 컨텍스트 속성 이름을 추가하십시오:

[General]
disableUnqualifiedAccess = "someOtherProperty,myProperty"
warnOnUsage = "someOtherProperty,myProperty"
disableHeuristic = false

.contextProperties.ini 형식에 대한 자세한 내용은 ‘컨텍스트 속성 설정’을 참조하십시오.

© 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.