πŸ’»
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

Was this helpful?

  1. QT QML
  2. QT QML Concept 2023

Using enumerations in QML without C ++

PreviousRegistering a QML Type as a Singleton objectNextRegistering a Singleton object to use β€œStatic” methods in QML

Last updated 1 year ago

Was this helpful?

Well, finally they finally got it !!! There was an opportunity to declare enums immediately in QML without dancing with C ++. How to add your . In fact, there is nothing complicated on the part of C ++, just need to write a class inherited from QObject, and register through Q_ENUM or Q_ENUMS your enumeration in this class, and register the class via qmlRegisterType.

Q_ENUM was added to Qt 5.5 to replace Q_ENUMS , and it allows QMetaEnum to be compiled at compile time using QMetaEnum::fromType . Such enumerations are now automatically registered as metatypes and can be converted to strings in QVariant or typed as a string using qDebug() .

However, according to my observations when registering transfers through Q_ENUM in QML files in Qt Creator, the code completion for these enumerations does not work, whereas when registering through Q_ENUMS everything works.

But now you can write an enumeration immediately in your custom QML type.

For example, let’s create this type of object in QML.

MyText.qml

import QtQuick 2.10
 
Text {
    enum MyEnum {
        First = 10,
        Second
    }
 
    anchors.centerIn: parent
}

Then in main.qml it can be used as follows

main.qml

import QtQuick 2.10
import QtQuick.Window 2.10
 
Window {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")
 
    MyText {
        text: MyText.MyEnum.Second
    }
}

The screen will display the number 11

As you can see, the syntax for accessing the enumeration is as follows.

. .

And it does not matter, inside the type you use this enumeration or outside it. That is, use within the type of this enumeration will look like this.

import QtQuick 2.10
 
Text {
    enum MyEnum {
        First = 10,
        Second
    }
 
    anchors.centerIn: parent
    text: MyText.MyEnum.Second
}

ATTENTION!!! – the following code will not work

import QtQuick 2.10
 
Text {
    enum MyEnum {
        First = 10,
        Second
    }
 
    anchors.centerIn: parent
    text: MyEnum.Second
}

That is, even inside a custom type, you need to refer to this type of enumeration through its name, that is, the name of the QML file.

I notice that from the point of view of organizing programs in QML, each QML file is a QML type, that is, a self-contained object that will inherit from the QObject class in the inheritance hierarchy at the C ++ level.

Also, unfortunately, the following enumeration options are not yet supported.

enum MyEnum {
    First = -1,     
    Second
}
 
enum MySecondEnum {
    First,     
    Second = First
}

Among other things, the highlighting of the enum syntax in QML is not yet supported in the actual at the time of writing the Qt Creator 4.5.0 article. However, the transfers themselves work.

πŸ§‘β€πŸ’»
πŸ“˜
enumerations to QML through C ++ I described in this article