Trying to use enum in QML. But the code does not work, the window is empty.

Application Output:

13:07:20 am: Starting X: \ QtPrograms \ build-EnumTest-Desktop-Debug \ debug \ EnumTest ... QML debugging is enabled. Only use this in a safe environment. qrc: /main.qml: 20: ReferenceError: main is not defined qrc: /main.qml: 18: ReferenceError: main is not defined

main.qml

import QtQuick 2.9 import QtQuick.Window 2.2 Window { visible: true width: 640 height: 480 title: qsTr("Hello World") Text { id: name enum TextType { Normal, Heading } property int textType: main.TextType.Normal text: textType === main.TextType.Normal ? "Normal" : "Heading" } } 
  • What is main ? It is not defined anywhere, and qt you naturally swear at it. First try to equate the type to zero and check if the Bearded Beaver will work
  • @BeardedBeaver main is the name of main.qml - Evgeny Druzhinin
  • Trying to do as in the example doc.qt.io/qt-5/… - Evgeny Druzhinin
  • This is just a description of the use of enum in qml, to run it you need to create a QtQuick application - Alexander Chernin
  • @AlexanderChernin I did it. This is a QtQuick Application. Just posted a fragment. - Evgeny Druzhinin

1 answer 1

It was necessary to add another qml file to the resources and use it in main.qml. The final code looks like this:

main.qml

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

MyText.qml

 import QtQuick 2.10 Text { id: name enum TextType { Normal, Heading } property int textType: MyText.TextType.Heading text: textType === MyText.TextType.Normal ? "Normal" : "Heading" }