Evgenii Legotckoi
Evgenii LegotckoiDec. 15, 2017, 4:02 a.m.

QML - Tutorial 035. Using enumerations in QML without C ++

Content

Well, finally they finally got it !!! There was an opportunity to declare enums immediately in QML without dancing with C ++. How to add your enumerations to QML through C ++ I described in this article . 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.

We recommend hosting TIMEWEB
We recommend hosting TIMEWEB
Stable hosting, on which the social network EVILEG is located. For projects on Django we recommend VDS hosting.

Do you like it? Share on social networks!

m
  • June 20, 2022, 8:26 a.m.

А как правильно зарегистрировать C++ enum, допустим

enum CppEnumType {
 Foo,
 Bar
};

чтобы использовать его в QML как

property CppEnumType: CppEnumType.Foo

У меня никак не получается тип проперти сделать enum из C++, только как int

Evgenii Legotckoi
  • June 21, 2022, 3:09 a.m.
i
  • Nov. 11, 2024, 6:23 p.m.

priligy cvs 24 Together with antibiotics such as amphotericin B 10, griseofulvin 11 and streptomycin 12, chloramphenicol 9 is in the World Health Organisation s List of Essential Medicines, the safest and most effective medicines needed in a health system

Comments

Only authorized users can post comments.
Please, Log in or Sign up
AD

C ++ - Test 004. Pointers, Arrays and Loops

  • Result:50points,
  • Rating points-4
m

C ++ - Test 004. Pointers, Arrays and Loops

  • Result:80points,
  • Rating points4
m

C ++ - Test 004. Pointers, Arrays and Loops

  • Result:20points,
  • Rating points-10
Last comments
i
innorwallNov. 14, 2024, 9:09 a.m.
Using variables declared in CMakeLists.txt inside C ++ files where can i buy priligy online safely Tom Platz How about things like we read about in the magazines like roid rage and does that really
i
innorwallNov. 12, 2024, 11:12 a.m.
Django - Tutorial 055. How to write auto populate field functionality Freckles because of several brand names retin a, atralin buy generic priligy
i
innorwallNov. 12, 2024, 7:23 a.m.
QML - Tutorial 035. Using enumerations in QML without C ++ priligy cvs 24 Together with antibiotics such as amphotericin B 10, griseofulvin 11 and streptomycin 12, chloramphenicol 9 is in the World Health Organisation s List of Essential Medici…
i
innorwallNov. 12, 2024, 4:50 a.m.
Qt/C++ - Lesson 052. Customization Qt Audio player in the style of AIMP It decreases stress, supports hormone balance, and regulates and increases blood flow to the reproductive organs buy priligy online safe Promising data were reported in a PDX model re…
i
innorwallNov. 12, 2024, 3:19 a.m.
Heap sorting algorithm The role of raloxifene in preventing breast cancer priligy precio
Now discuss on the forum
i
innorwallNov. 14, 2024, 7:52 a.m.
добавить qlineseries в функции PMID 35774217 Free PMC article priligy cvs
i
innorwallNov. 11, 2024, 11:55 p.m.
Всё ещё разбираюсь с кешем. priligy walgreens levitra dulcolax carbs The third ring was found to be made up of ultra relativistic electrons, which are also present in both the outer and inner rings
9
9AnonimOct. 25, 2024, 9:10 p.m.
Машина тьюринга // Начальное состояние 0 0, ,<,1 // Переход в состояние 1 при пустом символе 0,0,>,0 // Остаемся в состоянии 0, двигаясь вправо при встрече 0 0,1,>…

Follow us in social networks