한국어
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 86887
59 Windows에서 Qt D-Bus를 사용하여 프로세스간 통신(IPC) file makersweb 2019.05.02 4598
58 QML과 QtQuick 모듈 개념과 기본 타입들 makersweb 2019.04.26 13545
57 QML 전역 객체 (Global Object) file makersweb 2019.04.10 1115
56 tslib의 ts_calibrate를 응용해서 Qt로 터치보정기능 구현 file makersweb 2019.04.06 1419
55 GPU가 없는 장치에서 Qt Quick을 사용 makersweb 2019.04.02 1920
54 QTextCodec클래스를 사용하여 유니코드와 EUC-KR 변환 makersweb 2019.03.25 2975
53 qInstallMessageHandler를 이용한 디버그 메세지 출력 제어하기 makersweb 2019.02.25 1416
52 Qt5기반 독립 프로세스(out-of-process)로 동작하는 가상키보드(virtual keyboard) file makersweb 2019.02.24 2684
51 Qml 기본 컴포넌트 강좌 (4) - 모델 리스팅(Listing) file 운영자 2019.02.23 5413
50 Qt Bluetooth를 이용한 시리얼(Serial) 통신 file makersweb 2019.02.17 3681
49 Qml 기본 컴포넌트 강좌 (3) - 배치(positioning) 컴포넌트 file 운영자 2019.02.10 4980
48 QString 문자열 다루기 예제 운영자 2019.01.26 40971
47 Qt SQL을 이용한 가벼운 데이터베이스 다루기 [1] file 운영자 2019.01.23 7111
46 구글 클라우드 Speech-To-Text API를 Qt기반(C++, Qml)테스트 [7] file makersweb 2019.01.20 2699
45 QNetworkAccessManager를 통해 HTTP POST 하는 예제 makersweb 2019.01.17 4885
44 Qt응용프로그램 실행 시 콘솔창(터미널)같이 띄우기 file makersweb 2019.01.16 4583
43 안드로이드 가상장치 사용 file makersweb 2019.01.13 1129
42 Qml 기본 컴포넌트 강좌 (2) [2] file makersweb 2019.01.05 8754
41 Qml 기본 컴포넌트 강좌 (1) file makersweb 2019.01.03 12271
40 QtWayland와 ivi-compositor file makersweb 2018.12.27 2471