한국어
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 85952
139 Qml 기본 컴포넌트 강좌 (1) file makersweb 2019.01.03 12086
138 Qml 기본 컴포넌트 강좌 (2) [2] file makersweb 2019.01.05 8631
137 안드로이드 가상장치 사용 file makersweb 2019.01.13 1074
136 Qt응용프로그램 실행 시 콘솔창(터미널)같이 띄우기 file makersweb 2019.01.16 4490
135 QNetworkAccessManager를 통해 HTTP POST 하는 예제 makersweb 2019.01.17 4774
134 구글 클라우드 Speech-To-Text API를 Qt기반(C++, Qml)테스트 [7] file makersweb 2019.01.20 2620
133 Qt SQL을 이용한 가벼운 데이터베이스 다루기 [1] file 운영자 2019.01.23 6968
132 QString 문자열 다루기 예제 운영자 2019.01.26 40156
131 Qml 기본 컴포넌트 강좌 (3) - 배치(positioning) 컴포넌트 file 운영자 2019.02.10 4876
130 Qt Bluetooth를 이용한 시리얼(Serial) 통신 file makersweb 2019.02.17 3570
129 Qml 기본 컴포넌트 강좌 (4) - 모델 리스팅(Listing) file 운영자 2019.02.23 5310
128 Qt5기반 독립 프로세스(out-of-process)로 동작하는 가상키보드(virtual keyboard) file makersweb 2019.02.24 2628
127 qInstallMessageHandler를 이용한 디버그 메세지 출력 제어하기 makersweb 2019.02.25 1353
126 QTextCodec클래스를 사용하여 유니코드와 EUC-KR 변환 makersweb 2019.03.25 2893
125 GPU가 없는 장치에서 Qt Quick을 사용 makersweb 2019.04.02 1857
124 tslib의 ts_calibrate를 응용해서 Qt로 터치보정기능 구현 file makersweb 2019.04.06 1368
123 QML 전역 객체 (Global Object) file makersweb 2019.04.10 1049
122 QML과 QtQuick 모듈 개념과 기본 타입들 makersweb 2019.04.26 13407
121 Windows에서 Qt D-Bus를 사용하여 프로세스간 통신(IPC) file makersweb 2019.05.02 4483
120 가상키보드(Qt Virtual Keyboard)를 사용하는 방법 [32] file makersweb 2019.05.03 221168