한국어
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 116992
180 Qt Android 앱에 AdMob 배너달기 file makersweb 2021.12.04 2957
179 HTTPS URL을 연결할 때 SslHandshakeFailedError 오류 makersweb 2022.07.31 3037
178 Qt 6.4에 추가될 Qt Quick 3D Physics file makersweb 2022.08.07 3257
177 성능 고려 및 제안 사항 makersweb 2022.03.07 3309
176 QProcess 보안 권고 리뷰 file makersweb 2022.09.18 3338
175 그래픽 소프트웨어에서 디자인 내보내기 (Exporting Designs from Graphics Software) j2doll 2020.12.25 3505
174 Binding 타입으로 객체 속성 간 묶기 makersweb 2022.03.04 3525
173 Qt Marketplace 발표 makersweb 2019.12.02 3758
172 Qt Bluetooth Low Energy 개요 makersweb 2022.02.13 3774
171 QML에서 Websocket 서버와 통신 file makersweb 2021.09.18 3803
170 응용프로그램 자동실행 설정 (on Windows) makersweb 2021.05.08 3803
169 Qt 6 에서 프로퍼티 바인딩 makersweb 2021.04.03 3821
168 VirtualKeyboard 스타일 커스터 마이징 makersweb 2022.03.13 3866
167 Qt5Compat 라이브러리를 사용하여 Qt5에서 Qt6로 포팅 [2] makersweb 2020.12.05 3873
166 QML 에서 QR코드 생성 file makersweb 2021.08.20 3941
165 Qt로 작성된 안드로이드 APP에 Splash Screen을 추가 file makersweb 2020.03.10 3958
164 Qt Safe Renderer 개요 file makersweb 2022.09.08 3978
163 Qt 응용프로그램에서 Lottie Animation사용 file makersweb 2021.05.30 3982
162 Qt 응용프로그램에서 PDF 문서 렌더링 file makersweb 2021.09.23 3993
161 QtQuick 애플리케이션에 Rive 애니메이션 통합 makersweb 2024.03.31 4009