초기화 QML Runtime
QML 문서는 QML Runtime에 의해 로드되고 실행됩니다. 또한 QML Runtime은 다른 Qt Qml 모듈 및 유형에 대한 액세스도 제공합니다.
또한, Qt 프레임워크는 .qml 파일을 불러오는 qml 도구를 제공합니다. 이 도구는 QML Runtime을 불러오기 위해 C++ 애플리케이션을 작성할 필요 없이 QML 코드를 개발하고 테스트하는 데 유용합니다.
QML을 사용하는 애플리케이션을 실행하려면, 해당 애플리케이션에서 QML Runtime을 호출해야 합니다. QML Runtime을 호출하려면:
- Qt C++ 애플리케이션을 생성합니다.
응용 프로그램은 다음 방법 중 하나를 사용하여 QQmlEngine 을 불러와야 합니다:
- QQuickView 인스턴스를 통해 QML 파일을 로드합니다.
- QQmlEngine 인스턴스를 생성하고 ` QQmlComponent`를 사용하여 QML 파일을 로드합니다.
- Qt Bridge 애플리케이션을 생성합니다.
자세한 내용은 Qt Bridge 문서를 참조하십시오:
qml도구를 사용하여 QML 파일을 직접 불러와 실행할 수 있습니다.이 도구는 QML Runtime을 불러오기 위한 별도의 애플리케이션을 작성할 필요 없이 QML 코드를 개발하고 테스트하는 데 유용합니다. 자세한 내용은 ‘ QML Runtime 도구를 사용한 프로토타이핑’을 참조하십시오.
C++ 애플리케이션에서 QML Runtime 초기화
QQuickView를 사용한 초기화
QQuickView 는 QML 파일을 불러올 수 있는 QWindow 기반 클래스입니다. 예를 들어, application.qml 라는 QML 파일이 있다면 다음과 같이 표시됩니다:
import QtQuick
Rectangle { width: 100; height: 100; color: "red" }Qt 애플리케이션의 main.cpp 파일에서 다음과 같이 로드할 수 있습니다:
#include <QGuiApplication>
#include <QQuickView>
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQuickView view;
view.setSource(QUrl::fromLocalFile("application.qml"));
view.show();
return app.exec();
}이렇게 하면 application.qml 의 내용을 표시하는 QWindow 기반 뷰가 생성됩니다.
빌드 파일
` find_package() ` 명령어를 사용하여 ` Qt6 ` 패키지에서 필요한 모듈 구성 요소를 찾으세요:
find_package(Qt6 REQUIRED COMPONENTS Quick)
target_link_libraries(mytarget PRIVATE Qt6::Quick)자세한 내용은 ‘CMake를 사용한 빌드’ 개요를 참조하십시오.
qmake를 사용하여 빌드하도록 모듈을 구성하려면, 프로젝트의 .pro 파일에서 QT 변수의 값으로 해당 모듈을 추가하십시오:
QT += quick자세한 내용은 ‘프로젝트 파일 생성’을 참조하십시오.
QQmlEngine 직접 생성
application.qml 에 그래픽 컴포넌트가 없거나, 다른 이유로 QQuickView 사용을 피하고 싶은 경우, 대신 QQmlEngine 를 직접 생성할 수 있습니다. 이 경우, application.qml 는 뷰에 배치되는 대신 QQmlComponent 인스턴스로 로드됩니다:
#include <QGuiApplication>
#include <QQmlEngine>
#include <QQmlContext>
#include <QQmlComponent>
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQmlEngine engine;
QQmlContext *objectContext = new QQmlContext(engine.rootContext());
QQmlComponent component(&engine, "application.qml");
QObject *object = component.create(objectContext);
// ... delete object and objectContext when necessary
return app.exec();
}Qt Quick 의 그래픽 요소를 전혀 사용하지 않는다면, 위 코드에서 QGuiApplication 을 QCoreApplication 로 대체할 수 있습니다. 이렇게 하면 Qt GUI 모듈에 대한 의존성 없이 QML을 프로그래밍 언어로 사용할 수 있습니다.
© 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.