한국어
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 85700
20 Qt Creator 에서 GitHub Copilot 사용하기 file makersweb 2024.04.13 63
19 클라우드용 Qt file makersweb 2024.01.16 594
18 QMake 프로젝트를 CMake 프로젝트로 변환 with qmake2cmake makersweb 2022.09.17 979
17 qbs 사용 방법(Helloworld) file makersweb 2019.10.23 3092
16 Qt Creator에서 Qt의존성 라이브러리 자동복사하기 file makersweb 2019.10.19 2155
15 Windows에서 Qt 설치 따라하기 file makersweb 2019.10.14 30845
14 Qbs 프로젝트를 정의하기 위해 사용되는 몇가지 중요한 아이템들 file makersweb 2019.10.13 292
13 Qbs에 대한 소개와 설치하는 방법 makersweb 2019.10.09 1365
12 [Qt News] Qt6 Git 개발 초기 단계 시작하기 j2doll 2019.08.02 2326
» QML, 이미지, 폰트등을 바이너리 리소스로 만들기 makersweb 2019.06.24 3497
10 Qt Creator에서 임베디드 장치로 deploy설정(Custom Process Step) file makersweb 2019.06.15 2195
9 QtInstallerFramework로 온라인 설치프로그램(Online Installer)만드는 방법 [4] file makersweb 2019.05.28 6246
8 리눅스에서 Qt4.8기반 어플리케이션의 한글입력 file makersweb 2018.11.29 2427
7 Qt Installer Framework - 패키징, 설치프로그램 제작 file makersweb 2018.10.14 11636
6 Qml 어플리케이션 정적 빌드 file makersweb 2018.07.27 2130
5 Qt Bluetooth 관련 기능 확인 사항 makersweb 2018.07.10 761
4 Windows에서 라즈베리파이3 Qt 어플리케이션 개발 및 원격 실행 file makersweb 2018.02.23 6064
3 Windows환경에서 mingw로 Qt 5.10 정적(static)빌드 file makersweb 2018.02.01 5683
2 Qt 3D Studio 시작하기 file makersweb 2018.01.11 3817
1 Ubuntu Linux에서 Qt Creator 설치 file makersweb 2016.03.06 10661