한국어
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 118890
140 Qt 스마트 포인터 (QSharedPointer, QScopedPointer, QPointer) makersweb 2022.08.18 4994
139 QProcess 예제 (프로그램 재시작) file makersweb 2023.01.25 5028
138 Qt로 작성된 안드로이드 APP에 Splash Screen을 추가 file makersweb 2020.03.10 5094
137 qInstallMessageHandler를 이용한 디버그 메세지 출력 제어하기 makersweb 2019.02.25 5099
136 Qt 5.15 및 Qt 6의 출시 일정 makersweb 2020.04.09 5173
135 Qbs 프로젝트를 정의하기 위해 사용되는 몇가지 중요한 아이템들 file makersweb 2019.10.13 5185
134 Visual Studio Code용 Qt 확장팩 file makersweb 2024.10.09 5195
133 클라우드용 Qt file makersweb 2024.01.16 5201
132 Android 애플리케이션 서명 구성 file makersweb 2023.12.17 5240
131 Qt for Embedded Linux 화면출력 makersweb 2019.10.17 5269
130 Qt로 작성된 iOS 앱에서 시리얼 통신 file makersweb 2022.04.30 5278
129 Qt 하이브리드 애플리케이션(Hybrid App) 개발 file makersweb 2023.02.08 5474
128 Qt Quick Controls 2에 네이티브 데스크탑 스타일 추가 file makersweb 2020.11.23 5573
127 Qml에서 커튼효과 구현 예제 - Shader Effects file 운영자 2018.12.05 5602
126 Qt기반의 서버와 클라이언트간 SOAP(Simple Object Access Protocol) file makersweb 2020.05.11 5608
125 QRandomGenerator 클래스를 사용하여 난수(random values) 생성 makersweb 2020.10.17 5662
124 Qt Quick 3D 소개 makersweb 2019.11.09 5673
123 싱글터치 스크린 및 임베디드 리눅스 기반에서 Qt 터치입력 makersweb 2018.12.24 5725
122 Qt3D의 QML 타입으로 3D렌더링 file makersweb 2019.11.20 5803
121 Qt로 XML 파싱 : Qt 6에서 업데이트된 (Parsing XML with Qt: Updates for Qt 6) [1] j2doll 2020.01.16 5811