한국어
Qt
 

Qt응용프로그램을 개발할 때 사용되는 리소스(이미지, qml, 폰트등)를 바이너리로 형태로 만드는 방법을 설명한다.

 

Qt의 리소스 시스템은 qmake와 rcc를 기반으로 Resource Collection Files 이라고 불리는 XML 기반 파일 형식인 qrc에 포함된 리소스를 사용할 수 있게 해준다. qrc파일은 일반적으로 아래처럼 작성된다.

resource.qrc
<RCC>
    <qresource prefix="/qml">
        <file>myqml.qml</file>
    </qresource>
    <qresource prefix="/image">
        <file>logo.png</file>
    </qresource>
    <qresource prefix="/font">
        <file>NanumGothic-Regular.ttf</file>
    </qresource>
</RCC>

 

보통은 QtCreator에서 Qt Quick Application 프로젝트를 생성하면 .qrc파일이 포함된다.

 

이 qrc파일이 프로젝트파일(.pro)에 포함되면 리소스는 응용프로그램과 같이 컴파일 된다. 이 경우 리소스가 응용프로그램에 포함되므로 별도의 파일로 배포하고 싶지 않을 때 유용한 방법이다.

.pro
RESOURCES += \
    resource.qrc

 

rcc를 사용해서 리소스 파일만 별도의 바이너리 형태로 만들 수 도 있다.

예를 들어 위의 resource.qrc파일을 아래와 같이 rcc를 -binary 옵션으로 rcc파일을 만든다. (rcc는 Qt가 설치된 곳에 있다.)

rcc.exe -binary resource.qrc -o resource.rcc

 

만들어진 rcc파일을  아래의 예제처럼 사용한다.

main.qml
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QResource>

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);

    QQmlApplicationEngine engine;
    QResource::registerResource(qApp->applicationDirPath() + "/resource.rcc");
    const QUrl url(QStringLiteral("qrc:/qml/myqml.qml"));
    engine.load(url);

    return app.exec();
}

다음은 QML에서 prefix를 붙여 리소스에 엑세스하는 방법을 보여준다.

myqml.qml

import QtQuick 2.12
import QtQuick.Window 2.12

Window {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")

    FontLoader{
        id: myfont
        source: "qrc:/font/NanumGothic-Regular.ttf"
    }

    Image {
        id: logo
        anchors.centerIn: parent
        source: "qrc:/image/logo.png"
    }

    Text {
        id: text
        text: "나눔고딕폰트"
        font.family: myfont.name
        font.pixelSize: 12
        anchors.horizontalCenter: parent.horizontalCenter
    }
}
번호 제목 글쓴이 날짜 조회 수
공지 Qt프로그래밍(QtQuick) Beginner를 위한 글 읽는 순서 운영자 2019.01.05 116908
120 싱글터치 스크린 및 임베디드 리눅스 기반에서 Qt 터치입력 makersweb 2018.12.24 5161
119 Qbs에 대한 소개와 설치하는 방법 makersweb 2019.10.09 5168
118 최초의 Qt 6.0 스냅샷 제공 (First Qt 6.0 Snapshot Available) j2doll 2020.06.21 5173
117 웹기반 Qt Design Viewer [2] file makersweb 2019.10.23 5207
116 QRandomGenerator 클래스를 사용하여 난수(random values) 생성 makersweb 2020.10.17 5228
115 Q_D매크로와 d-pointer file makersweb 2019.05.07 5255
114 Qt기반의 서버와 클라이언트간 SOAP(Simple Object Access Protocol) file makersweb 2020.05.11 5292
113 clazy 로 13개의 시그널, 슬롯 오류 해결 makersweb 2022.08.23 5352
112 Qml에서 커튼효과 구현 예제 - Shader Effects file 운영자 2018.12.05 5391
111 QPA 플러그인과 HTML5 Backend file makersweb 2017.12.27 5435
110 QThread 및 QMutex 예제 makersweb 2021.01.12 5440
109 VTK 를 사용해서 강력한 시각화(3D, Plotting, Chart)Qt 응용프로그램 개발하기 file makersweb 2022.10.16 5548
108 Qt3D의 QML 타입으로 3D렌더링 file makersweb 2019.11.20 5560
107 Loader를 사용하여 동적으로 QML 로드 makersweb 2021.01.19 5587
106 [Qt] Google Play의 향후 요구 사항을 준수하는 방법 [2] j2doll 2019.07.29 5588
105 앱을 종료할 때 QML 바인딩 오류를 피하는 방법 makersweb 2021.08.08 5651
104 QML의 사용자 정의 Image makersweb 2023.09.17 5748
103 안드로이드용 Qt 6.2 makersweb 2021.10.02 5771
102 싱글 샷(Single-Shot) 시그널/슬롯 연결 makersweb 2021.05.12 5772
101 Qt5기반 독립 프로세스(out-of-process)로 동작하는 가상키보드(virtual keyboard) file makersweb 2019.02.24 5788