한국어
Qt
 

C++의 열거형(enum)을 QML에서 사용하는 방법을 설명한다.

 

먼저 QObject를 상속받는 클래스와 enum을 정의한다.

class MyEnums : public QObject
{
    Q_OBJECT
public:
    explicit MyEnums(QObject *parent = nullptr);

    enum CallStatus{
        Dialed = 0,
        Cancelled,
        Received,
        Missed
    };

    Q_ENUMS (CallStatus)
};

 

main.cpp

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include "myenums.h"

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

    // 정의한 enum을 qml 타입으로 등록
    qmlRegisterType<MyEnums>("MyEnum", 1, 0, "MyEnum");

    QQmlApplicationEngine engine;
    const QUrl url(QStringLiteral("qrc:/main.qml"));
    QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                     &app, [url](QObject *obj, const QUrl &objUrl) {
        if (!obj && url == objUrl)
            QCoreApplication::exit(-1);
    }, Qt::QueuedConnection);
    engine.load(url);

    return app.exec();
}

 

QML에서 사용

import QtQuick 2.13
import MyEnum 1.0 // 등록한 타입을 import 한다.

Item {
    visible: true
    width: 640
    height: 480

    function getStatus(status){
        switch(status){
        case MyEnum.Cancelled:
            return "Cancelled"
        case MyEnum.Dialed:
            return "Dialed"
        case MyEnum.Received:
            return "Received"
        default:
            return ""
        }
    }

    Text {
        id: myType
        text: getStatus(MyEnum.Dialed)
        anchors.centerIn: parent
    }
}

 

QMetaEnum 클래스는 열거형에 대한 meta-data를 제공한다.

#include <QObject>
#include <QMetaEnum>

class MyEnums : public QObject
{
    Q_OBJECT
public:
    explicit MyEnums(QObject *parent = nullptr);

    enum CallStatus{
        Dialed = 0,
        Cancelled,
        Received,
        Missed
    };

    Q_ENUMS (CallStatus)

    static QString callStatusString(CallStatus type){
        const QMetaObject &mo = MyEnums::staticMetaObject;
        return QString(mo.enumerator(mo.indexOfEnumerator("CallStatus")).valueToKey(type));
    }
};

 

다음 처럼 QString문자열로 얻을 수 있다.

QString status = MyEnums::callStatusString(MyEnums::CallStatus::Received);
번호 제목 글쓴이 날짜 조회 수
공지 Qt프로그래밍(QtQuick) Beginner를 위한 글 읽는 순서 운영자 2019.01.05 116992
100 Qt 응용프로그램에 Web 구성 요소를 표시 with Servo file makersweb 2024.04.27 6353
99 Qt로 데이터를 직렬화(serialization)하는 방법 makersweb 2020.08.04 6306
98 ShaderEffect QML Type 을 이용한 그래픽효과 file makersweb 2018.12.09 6293
97 Qml 어플리케이션 정적 빌드 file makersweb 2018.07.27 6263
96 [Qt News] Qt for Python을 위한 기술 비전 j2doll 2019.08.20 6237
95 QML에서 멀티 스레드(multithreading) 프로그래밍 file makersweb 2019.05.25 6195
94 Qt Creator 에서 GitHub Copilot 사용하기 file makersweb 2024.04.13 6157
93 Qt 6.0의 개발 호스트 및 대상 플랫폼 makersweb 2020.09.16 6138
92 [Qt News] Qt 6 기술 비전 (Technical vision for Qt 6) [2] j2doll 2019.08.08 6131
91 QML에서 undefined를 확인하는 방법 makersweb 2017.11.29 6056
90 [Qt News] Qt6 Git 개발 초기 단계 시작하기 j2doll 2019.08.02 6050
89 많은 리소스를 사용하는 Qt프로젝트에서 고려해봐야 할 qmake 옵션 makersweb 2019.10.11 6034
88 QOpenVirtualkeyboard(Qt 5용 한글 및 영문, 숫자 가상키보드) file makersweb 2019.11.27 5989
87 라즈베리파이3에서 Boot to Qt 실행해보기 makersweb 2019.11.13 5943
86 QML과 JavaScript 의 숫자 관련 내장된 함수 makersweb 2021.03.28 5926
85 GPU가 없는 장치에서 Qt Quick을 사용 makersweb 2019.04.02 5911
84 OpenGL 렌더링을 QtQuick과 통합하는 방법 file makersweb 2019.10.01 5896
83 소스코드에서 환경변수 가져오기와 설정하기 makersweb 2018.10.08 5894
82 Qt for MCU 1.0 릴리즈 makersweb 2019.12.10 5846
81 Qt MQTT의 pus/sub 튜토리얼 file makersweb 2021.02.06 5835