πŸ’»
Programming Concept
  • πŸ‘¨β€πŸ”¬About Me
  • πŸ“—C++ STL Concept 2023
    • πŸ“–STL in CPP ( Part I)
    • πŸ“–STL in CPP ( Part II)
    • πŸ“–STL in CPP ( Part III )
    • πŸ“–STL in CPP ( Part IV )
    • πŸ“–STL in CPP ( Part V )
    • πŸ“–STL in CPP ( Part VI )
    • πŸ“–STL in CPP ( Part VII )
  • ✏️Pointers in C / C++
    • πŸ“–Pointers in details
  • πŸ“˜Strings in CPP 2023
  • πŸ•žThread in C++ 2023
  • πŸ““Smart Pointer in C++ 2023
  • Basic C++ Topics
    • πŸ’ΌType Casting in C++ 2023
  • πŸ’»Advanced C++ Programming
    • πŸŽ“Topics (Syllabus)
    • πŸ§‘β€πŸŽ“Inheritance and Polymorphism
    • πŸ–ŠοΈException Handling
    • πŸƒRuntime Type Information
    • πŸ“”An Overview of Templates
    • πŸ“‘Template in C++
  • πŸ’»Embedded Linux
    • πŸ–₯️Embedded Linux OS
      • Build Yocto Project Step By Step
      • How to install apt-get to the Yocto Project image
      • Installing gcc/g++ toolchain in yocto project
      • Installing GDB in yocto project
      • Installing ssh-server-dropbear for embedded linux in yocto project
      • Add Python To A Build
      • Add Boost To A Build
      • Adding Valgrind To Build
      • How To Add A Custom App To A Yocto Build
      • Raspberry Pi
    • πŸ“ΆCommunication Protocols
      • ✏️Working With I2C using c++
      • πŸ“”SPI Implementation with source code in C++
      • πŸ““Linux Serial Ports Using C/C++
      • πŸ–±οΈUART
      • πŸ–¨οΈUniversal GPIO Access
  • πŸ§‘β€πŸ’»QT QML
    • πŸ“˜QT QML Concept 2023
      • Why Qt Framework and QML
      • Sqlite3 for qtquick application
      • How To Install sqlite3 in windows 11 || Windows 10
      • Signals and Slots in Qt QML
      • Working with Signals and Slots in QML
      • Sorting QML ListModels
      • ListView In QML With Section Delegate
      • Registration of custom enum in QML
      • Registering a QML Type as a Singleton object
      • Using enumerations in QML without C ++
      • Registering a Singleton object to use β€œStatic” methods in QML
      • Connecting JavaScript files to other JavaScript files in a Qt / QML project
      • Positioning in Qt QML with anchors
      • TextInput validators in Qt Qml
      • Qt Qml Abstract Item Model using C++
      • Qt QML List Model
      • How to Register your C++ Class as a QML Type
      • Jabra Speaker Connect Qt Project
      • Qt | One article summarizing QObject
  • ▢️QT Concept
    • ℹ️Icon button
    • πŸ’»Register QML Singletone in CP
Powered by GitBook
On this page
  • Info.h
  • Main.cpp
  • Main.qml

Was this helpful?

  1. QT QML
  2. QT QML Concept 2023

Registration of custom enum in QML

In order to use enum enum enumerations in Q ++, you need to create a class inherited from QObject and register it as a QML Type before running the QML engine in the application.

To learn, create a project using QtQuick.

The minimum version of this class with enumerations will be as follows:

Info.h

#ifndef INFO_H
#define INFO_H
 
#include <QObject>
 
class Info : public QObject
{
    Q_OBJECT
public:
    enum State {
        Information,
        Debug,
        Warning,
        Error
    };
    Q_ENUM(State)
};
 
#endif // INFO_H

Next, you need to register this class as a QML Type in the main function. This requires a QtQml header file and a qmlRegisterType function.

Main.cpp

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QtQml> // Include QtQml to use qmlRegisterType
 
#include "info.h" // Connect the header file of the Info class
 
int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);
 
    qmlRegisterType<Info>("info", 1, 0, "Info"); // Register this class as a module
 
    QQmlApplicationEngine engine;
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    if (engine.rootObjects().isEmpty())
        return -1;
 
    return app.exec();
}

After that it will only be necessary to connect the new QML module in the main.qml file and use the enumerations to display the text. We will output the message β€œMessage 0”, β€œMessage 1”, etc. The numbers will be taken from the enumerations.

Main.qml

import QtQuick 2.5
import QtQuick.Window 2.2
import QtQuick.Controls 1.4
 
import info 1.0  // Import Module
 
Window {
    visible: true
    width: 360
    height: 360
    title: qsTr("Enumeration")
 
    ListView {
        anchors.fill: parent
        delegate: Item {
            height: 48
            width: parent.width
            Text {
                anchors.fill: parent
                text: "Message " + model.text
 
                verticalAlignment: Text.AlignVCenter
                horizontalAlignment: Text.AlignHCenter
            }
        }
 
        model: ListModel {
            // Use the enumerations from C ++
            ListElement {text: Info.Information}
            ListElement {text: Info.Debug}
            ListElement {text: Info.Warning}
            ListElement {text: Info.Error}
        }
    }
}

Appearance of the application will be as follows:

PreviousListView In QML With Section DelegateNextRegistering a QML Type as a Singleton object

Last updated 1 year ago

Was this helpful?

πŸ§‘β€πŸ’»
πŸ“˜