조회 수 10761 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄

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);
TAG •

List of Articles
번호 분류 제목 글쓴이 날짜 조회 수
공지 QML and Qt Quick Qt프로그래밍(QtQuick) Beginner를 위한 글 읽는 순서 운영자 2019.01.05 131107
80 General and Desktop 컨테이너에 적재된 객체를 편리하게 삭제하기 makersweb 2019.09.18 7037
79 QML and Qt Quick C++로 구현된 모델을 QML의 ListView에서 참조 file makersweb 2019.09.07 10323
78 QML and Qt Quick QSocketNotifier로 파일 디스크립터의 활동감지 makersweb 2019.08.28 9186
77 Mobile and Embedded MCU용 Qt에 대해서 makersweb 2019.08.22 7414
76 General and Desktop [Qt News] Qt for Python을 위한 기술 비전 j2doll 2019.08.20 10132
» QML and Qt Quick 열거형(enum)을 QML에서 사용하는 방법과 문자열(QString)로 얻기 makersweb 2019.08.20 10761
74 General and Desktop [Qt News] Qt 6 기술 비전 (Technical vision for Qt 6) 2 j2doll 2019.08.08 8176
73 Installation and Deployment [Qt News] Qt6 Git 개발 초기 단계 시작하기 j2doll 2019.08.02 9607
72 QML and Qt Quick [Qt] Google Play의 향후 요구 사항을 준수하는 방법 2 j2doll 2019.07.29 9176
71 General and Desktop Qt기반의 오픈소스 프로젝트들 - 2 운영자 2019.07.21 11745
70 Installation and Deployment QML, 이미지, 폰트등을 바이너리 리소스로 만들기 makersweb 2019.06.24 8391
69 Installation and Deployment Qt Creator에서 임베디드 장치로 deploy설정(Custom Process Step) file makersweb 2019.06.15 9173
68 QML and Qt Quick Qt Quick Controls 2사용 및 스타일 설정 file makersweb 2019.06.07 11950
67 QML and Qt Quick QML 강좌 - 동적 Listing (ListView) file makersweb 2019.06.01 15146
66 Installation and Deployment QtInstallerFramework로 온라인 설치프로그램(Online Installer)만드는 방법 4 file makersweb 2019.05.28 12635
65 QML and Qt Quick QtCreator Design으로 GUI만들기 (QML로 만드는 Hello World -2) 1 file makersweb 2019.05.26 23633
64 QML and Qt Quick QML에서 멀티 스레드(multithreading) 프로그래밍 file makersweb 2019.05.25 8085
63 General and Desktop QtSerialPort를 사용한 시리얼(Serial)통신 3 makersweb 2019.05.21 21359
62 General and Desktop Qt기반의 오픈소스 프로젝트들 makersweb 2019.05.15 11061
61 General and Desktop Q_D매크로와 d-pointer file makersweb 2019.05.07 7528
Board Pagination Prev 1 4 5 6 7 8 9 Next
/ 9