조회 수 6786 추천 수 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 •

  1. No Image notice

    Qt프로그래밍(QtQuick) Beginner를 위한 글 읽는 순서

    Date2019.01.05 CategoryQML and Qt Quick By운영자 Views110094
    read more
  2. No Image

    컨테이너에 적재된 객체를 편리하게 삭제하기

    Date2019.09.18 CategoryGeneral and Desktop Bymakersweb Views3445
    Read More
  3. C++로 구현된 모델을 QML의 ListView에서 참조

    Date2019.09.07 CategoryQML and Qt Quick Bymakersweb Views7826
    Read More
  4. No Image

    QSocketNotifier로 파일 디스크립터의 활동감지

    Date2019.08.28 CategoryQML and Qt Quick Bymakersweb Views4529
    Read More
  5. No Image

    MCU용 Qt에 대해서

    Date2019.08.22 CategoryMobile and Embedded Bymakersweb Views4097
    Read More
  6. [Qt News] Qt for Python을 위한 기술 비전

    Date2019.08.20 CategoryGeneral and Desktop Byj2doll Views4353
    Read More
  7. No Image

    열거형(enum)을 QML에서 사용하는 방법과 문자열(QString)로 얻기

    Date2019.08.20 CategoryQML and Qt Quick Bymakersweb Views6786
    Read More
  8. No Image

    [Qt News] Qt 6 기술 비전 (Technical vision for Qt 6)

    Date2019.08.08 CategoryGeneral and Desktop Byj2doll Views4597
    Read More
  9. No Image

    [Qt News] Qt6 Git 개발 초기 단계 시작하기

    Date2019.08.02 CategoryInstallation and Deployment Byj2doll Views4458
    Read More
  10. No Image

    [Qt] Google Play의 향후 요구 사항을 준수하는 방법

    Date2019.07.29 CategoryQML and Qt Quick Byj2doll Views3413
    Read More
  11. No Image

    Qt기반의 오픈소스 프로젝트들 - 2

    Date2019.07.21 CategoryGeneral and Desktop By운영자 Views7062
    Read More
  12. No Image

    QML, 이미지, 폰트등을 바이너리 리소스로 만들기

    Date2019.06.24 CategoryInstallation and Deployment Bymakersweb Views6189
    Read More
  13. Qt Creator에서 임베디드 장치로 deploy설정(Custom Process Step)

    Date2019.06.15 CategoryInstallation and Deployment Bymakersweb Views4971
    Read More
  14. Qt Quick Controls 2사용 및 스타일 설정

    Date2019.06.07 CategoryQML and Qt Quick Bymakersweb Views8931
    Read More
  15. No Image

    QML 강좌 - 동적 Listing (ListView)

    Date2019.06.01 CategoryQML and Qt Quick Bymakersweb Views12555
    Read More
  16. QtInstallerFramework로 온라인 설치프로그램(Online Installer)만드는 방법

    Date2019.05.28 CategoryInstallation and Deployment Bymakersweb Views8616
    Read More
  17. QtCreator Design으로 GUI만들기 (QML로 만드는 Hello World -2)

    Date2019.05.26 CategoryQML and Qt Quick Bymakersweb Views18789
    Read More
  18. No Image

    QML에서 멀티 스레드(multithreading) 프로그래밍

    Date2019.05.25 CategoryQML and Qt Quick Bymakersweb Views4895
    Read More
  19. No Image

    QtSerialPort를 사용한 시리얼(Serial)통신

    Date2019.05.21 CategoryGeneral and Desktop Bymakersweb Views16316
    Read More
  20. No Image

    Qt기반의 오픈소스 프로젝트들

    Date2019.05.15 CategoryGeneral and Desktop Bymakersweb Views7805
    Read More
  21. No Image

    Q_D매크로와 d-pointer

    Date2019.05.07 CategoryGeneral and Desktop Bymakersweb Views3643
    Read More
Board Pagination Prev 1 4 5 6 7 8 9 Next
/ 9