Some enums from a C++ namespace have undefined values in QML
-
I have defined a namespace with enums in C++ to use both in C++ and QML code. Some of the values work fine but others give undefined value.
There are around 30 enums in the namespace. Some have up to around 500 values.
I've defined them all in the same way:namespace AppEnumsNs { Q_NAMESPACE typedef enum EAppGUI_HPsIDs { APP_GUI_HP1_ID, APP_GUI_HP2_ID, APP_GUI_HP3_ID, APP_GUI_HP4_ID, APP_GUI_MAXNUM_HP } cAppGUI_HPsIds; Q_ENUM_NS(EAppGUI_HPsIDs) ... }
I've registered the namespace by
qmlRegisterUncreatableMetaObject
.This is the main function:
int main(int argc, char *argv[]) { set_qt_environment(); QGuiApplication app(argc, argv); QQmlApplicationEngine engine; const QUrl url(u"qrc:Main/main.qml"_qs); QObject::connect( &engine, &QQmlApplicationEngine::objectCreated, &app, [url](QObject *obj, const QUrl &objUrl) { if (!obj && url == objUrl) QCoreApplication::exit(-1); }, Qt::QueuedConnection); engine.addImportPath(QCoreApplication::applicationDirPath() + "/qml"); engine.addImportPath(":/"); qmlRegisterUncreatableMetaObject(AppEnumsNs::staticMetaObject, "define.enums", 1, 0, "AppEnumsNs", "Access to enums only"); CallbackWrapper cbkWpr; engine.rootContext()->setContextProperty("cbkWpr", &cbkWpr); engine.rootContext()->setContextProperty("wdgWpr", &wdgWpr); engine.load(url); if (engine.rootObjects().isEmpty()) { return -1; } return app.exec(); }
In the QML files I've imported the module define.enums 1.0
And while some enums work correctly others give undefined values.
Someone knows the reason for the undefined values? -
@fcarney It's true that the misspelled enums give the same problem but this is not the cause.
I created a simpler project that reproduces the problem. It displays an enum value in a text widget.
I created the project in Qt Design Studio. Generated the CMake Build Files. Opened it in Qt Creator.
Added two files:
src/AppGuiInterfaceStructure.h
src/AppGuiInterfaceStructure.cpp.
Edited files:
src/main.cpp
content/App.qml
content/Screen01.ui.qmlAppGuiInterfaceStructure.h
#ifndef APPGUIINTERFACESTRUCTURE_H_ #define APPGUIINTERFACESTRUCTURE_H_ #include <QObject> namespace AppEnumsNs { Q_NAMESPACE typedef enum EAppGUI_HPsIDs { APP_GUI_HP1_ID, APP_GUI_HP2_ID, APP_GUI_HP3_ID, APP_GUI_HP4_ID, APP_GUI_MAXNUM_HP } cAppGUI_HPsIds; Q_ENUM_NS(EAppGUI_HPsIDs) // Widgets de la Interface typedef enum EAppGUIwidgetIDlist { //PANTALLA DE CARGA imgLoadScrBckGnd, imgLoadAnim1, imgLoadAnim2, imgLoadAnim3, imgLoadAnim4, imgLoadAnim5, imgLoadAnim6, imgLoadAnim7, imgLoadAnim8, //PANTALLA DE INICIO imgInitScrBckGnd, butSelectWoman, butSelectMan, imgInitGPRScoverage, //V7790 //Menu principal butInitScrShowMainMenu, butInitScrHideMainMenu, imgInitScrHome, butInitScrCfg, butInitScrCalc, butInitScrData, swInitScrAvisos, imgInitScrNotificacion, popupInitScrAvisos, imgPopUpInitScrAvisoBckGND, imgPopUpInitScrAvisoNivelAguaBajo, imgPopUpInitScrAvisoNivelAguaMedio, imgPopUpInitScrAvisoTempAmb, imgPopUpInitScrAvisoRFID, imgPopUpInitScrAvisoRTC, imgPopUpInitScrAvisoFiltroAire, //PANTALLA DE TRATAMIENTO imgTrtScrBckGnd, imgFullBodyAndZoom, imgTrtGPRScoverage, //V7790 //Fondos de los paneles de control imgCtrlPnlBckGndHP1, imgCtrlPnlBckGndHP2, imgCtrlPnlBckGndHP3, imgCtrlPnlBckGndHP4, //Colores fondos de los paneles de control imgColourCtrlPnlBckGndHP1, imgColourCtrlPnlBckGndHP2, imgColourCtrlPnlBckGndHP3, imgColourCtrlPnlBckGndHP4, //Indicadores de accion del HP cuando activos, los inactivos forman parte del fondo imgCtrlPnlRFIDblinkHP1, imgCtrlPnlRFIDblinkHP2, imgCtrlPnlRFIDblinkHP3, imgCtrlPnlRFIDblinkHP4, imgCtrlPnlPulBlinkHP1, imgCtrlPnlPulBlinkHP2, imgCtrlPnlPulBlinkHP3, imgCtrlPnlPulBlinkHP4, imgCtrlPnlVacBlinkHP1, imgCtrlPnlVacBlinkHP2, imgCtrlPnlVacBlinkHP3, imgCtrlPnlVacBlinkHP4, imgCtrlPnlCoolingBlinkHP1, imgCtrlPnlCoolingBlinkHP2, imgCtrlPnlCoolingBlinkHP3, imgCtrlPnlCoolingBlinkHP4, //Botones del panel de control de PLAY y Test Vacum butCtrlPnlPlayHP1, butCtrlPnlPlayHP2, butCtrlPnlPlayHP3, butCtrlPnlPlayHP4, swCtrlPnlTstVacHP1, swCtrlPnlTstVacHP2, swCtrlPnlTstVacHP3, swCtrlPnlTstVacHP4, //Botones (switch) que permite seleccionar modo de tratamiento AUTO o MANUAL butSwtchModeAutoManualHP1, butSwtchModeAutoManualHP2, butSwtchModeAutoManualHP3, butSwtchModeAutoManualHP4, //Visores de tiempo restante de trt y boton de seleccion de tiempo numVwrTimeHP1, numVwrTimeHP2, numVwrTimeHP3, numVwrTimeHP4, butSelectTimeHP1, butSelectTimeHP2, butSelectTimeHP3, butSelectTimeHP4, butVwrTemperatureHP1, butVwrTemperatureHP2, butVwrTemperatureHP3, butVwrTemperatureHP4, //Visor numerico para contabilizar resucciones numVwrResucctionCountHP1, numVwrResucctionCountHP2, numVwrResucctionCountHP3, numVwrResucctionCountHP4, //Boton con la imagen e imagen con el texto del modelo imgModelTextHP1, imgModelTextHP2, imgModelTextHP3, imgModelTextHP4, butModelImageHP1, butModelImageHP2, butModelImageHP3, butModelImageHP4, //Botonoes de recogida y despliegue del panel de control butHideControlPanelHP1, butHideControlPanelHP2, butHideControlPanelHP3, butHideControlPanelHP4, butShowControlPanelHP1, butShowControlPanelHP2, butShowControlPanelHP3, butShowControlPanelHP4, //Botones de selección de Full body butFullBodyTorso, butFullBodyPiernas, butFullBodyBrazos, butFullBodyEspalda, butFullBodyPiernaMuslo, butFullBodyCuello, //Puntos seleccion de zona de tratamiento cuando zoom swDotZone0, swDotZone1, swDotZone2, swDotZone3, swDotZone4, swDotZone5, swDotZone6, swDotZone7, swDotZone8, swDotZone9, swDotZone10, swDotZone11, swDotZone12, swDotZone13, swDotZone14, //Punto seleccionado para tratamiento con ese HP a mostrar en full body imgSelectedTrtZoneHP1, imgSelectedTrtZoneHP2, imgSelectedTrtZoneHP3, imgSelectedTrtZoneHP4, //POPUP's //Common butPopUpReturn, imgPopUpHPcolorBar, //PopUp Time widgets popUpTime, butPopUpTime50min, butPopUpTime70min, imgPopUpTime1sel, imgPopUpTime2sel, //PopUp Test Vacum widgets popUpTstVac, imgPopUpTstVacBckGnd, imgPopUpTstVacLowLvl, imgPopUpTstVacMidLvl, imgPopUpTstVacHighLvl, imgPopUpTstVacPulHand, imgPopUpTstVacPulHandx2, butPopUpTstVacManualLowLvl, butPopUpTstVacManualMidLvl, butPopUpTstVacManualHighLvl, butPopUpTstVacAuto, swPopUpTstVacLowLvl, swPopUpTstVacMidLvl, swPopUpTstVacHighLvl, imgPopUpTstVacAUTOstatus, //PopUp Test Vacuum 2 widgets popUpTstVacSmall, imgPopUpVacSelBck, butPopUpTstVacSmallLowLvl, butPopUpTstVacSmallMedLvl, butPopUpTstVacSmallHighLvl, //PopUp RFID widgets popUpRFID, imgPopUpRFIDbckGnd, imgPopUpRFIDlabel, imgPopUpRFIDresult, butPopUpRFIDreuseLabel, numVwrPopUpRFIDrestTime, //PopUp Recommendation widgets swPopUpRecommInfo, popUpRecomm, imgPopUpRecommbckGnd, imgPopUpRecommCurved, imgPopUpRecommTight, imgPopUpRecommStright, imgPopUpRecommTiny, imgPopUpRecommTinyCurved, imgPopUpRecommDouble, imgPopUpRecommOval, imgPopUpRecommOvalCurved, imgPopUpRecommAngled, //Menu Principal butTrtScrShowMainMenu, butTrtScrHideMainMenu, butTrtScrHome, butTrtScrCfg, butTrtScrCalc, butTrtScrData, swTrtScrAvisos, imgTrtScrNotificacion, popupTrtScrAvisos, imgPopUpTrtScrAvisoBckGND, imgPopUpTrtScrAvisoNivelAguaBajo, imgPopUpTrtScrAvisoNivelAguaMedio, imgPopUpTrtScrAvisoTempAmb, imgPopUpTrtScrAvisoRFID, imgPopUpTrtScrAvisoRTC, imgPopUpTrtScrAvisoFiltroAire, //popUp's Error imgPopUpErrBckGndHP1, imgPopUpErrBckGndHP2, imgPopUpErrBckGndHP3, imgPopUpErrBckGndHP4, imgPopUpErrBckGndUC, numVwrPopUpErrHP1, numVwrPopUpErrHP2, numVwrPopUpErrHP3, numVwrPopUpErrHP4, numVwrPopUpErrUC, imgPopUpErrMsgHP1, imgPopUpErrMsgHP2, imgPopUpErrMsgHP3, imgPopUpErrMsgHP4, imgPopUpErrMsgUC, butPopUpErrOkHP1, butPopUpErrOkHP2, butPopUpErrOkHP3, butPopUpErrOkHP4, butPopUpErrOkUC, numVwrPopUpErrTRTtimeHP1, numVwrPopUpErrTRTtimeHP2, numVwrPopUpErrTRTtimeHP3, numVwrPopUpErrTRTtimeHP4, //PopUp's CleanUp imgPopUpCleanBckGndHP1, imgPopUpCleanBckGndHP2, imgPopUpCleanBckGndHP3, imgPopUpCleanBckGndHP4, numVwrPopUpCleanHP1, numVwrPopUpCleanHP2, numVwrPopUpCleanHP3, numVwrPopUpCleanHP4, butPopUpCleanHP1, butPopUpCleanHP2, butPopUpCleanHP3, butPopUpCleanHP4, butPopUpCleanYESHP1, butPopUpCleanYESHP2, butPopUpCleanYESHP3, butPopUpCleanYESHP4, butPopUpCleanNOHP1, butPopUpCleanNOHP2, butPopUpCleanNOHP3, butPopUpCleanNOHP4, imgPopUpCleanHP1, imgPopUpCleanHP2, imgPopUpCleanHP3, imgPopUpCleanHP4, butPopUpCleanCancelHP1, butPopUpCleanCancelHP2, butPopUpCleanCancelHP3, butPopUpCleanCancelHP4, numVwrPopUpCleanNumCancelHP1, numVwrPopUpCleanNumCancelHP2, numVwrPopUpCleanNumCancelHP3, numVwrPopUpCleanNumCancelHP4, //Pantalla de la calculadora imgFondoCalcScreen, imgCuerpoCalc, numVwrPantallaCalc, butNum0Calc, butNum1Calc, butNum2Calc, butNum3Calc, butNum4Calc, butNum5Calc, butNum6Calc, butNum7Calc, butNum8Calc, butNum9Calc, butMasCalc, butMenosCalc, butEnterCalc, butCECalc, butComaCalc, //Menu principal - calculadora butCalcScrShowMainMenu, butCalcScrHideMainMenu, butCalcScrHome, butCalcScrCfg, imgCalcScrCalc, butCalcScrData, swCalcScrAvisos, imgCalcScrNotificacion, popupCalcScrAvisos, imgPopUpCalcScrAvisoBckGND, imgPopUpCalcScrAvisoNivelAguaBajo, imgPopUpCalcScrAvisoNivelAguaMedio, imgPopUpCalcScrAvisoTempAmb, imgPopUpCalcScrAvisoRFID, imgPopUpCalcScrAvisoRTC, imgPopUpCalcScrAvisoFiltroAire, //Pantalla de configuración imgFondoCfgScreen, imgVentanaCfgCastellano, butCfgActualizarSoftware, imgSoftwareProgressionUpdateBar, numVwrModeloMaqCfg, numVwrVersionSoftwareCfg, butCfgRecargaCoolant, swCfgResuctionViewer, swCfgTemperatureViewer, butCfgCleanUpHP1, butCfgCleanUpHP2, butCfgCleanUpHP3, butCfgCleanUpHP4, butCfgCleanUpRoundHP, imgCfgGPRScoverage, //V7790 //Menu principal - configuracion butCfgScrShowMainMenu, butCfgScrHideMainMenu, butCfgScrHome, imgCfgScrCfg, butCfgScrCalc, butCfgScrData, swCfgScrAvisos, imgCfgScrNotificacion, popupCfgScrAvisos, imgPopUpCfgScrAvisoBckGND, imgPopUpCfgScrAvisoNivelAguaBajo, imgPopUpCfgScrAvisoNivelAguaMedio, imgPopUpCfgScrAvisoTempAmb, imgPopUpCfgScrAvisoRFID, imgPopUpCfgScrAvisoRTC, imgPopUpCfgScrAvisoFiltroAire, //Pantalla de idiomas imgFondoLanScreen, swLanFrances, swLanCastellano, swLanIngles, swLanItaliano, swLanAleman, swLanPolaco, swLanRuso, swLanPortugues, swLanChino, swLanJapones, swLanCoreano, //Menu principal - idiomas butLanScrShowMainMenu, butLanScrHideMainMenu, butLanScrHome, butLanScrCfg, butLanScrCalc, imgLanScrData, swLanScrAvisos, imgLanScrNotificacion, popupLanScrAvisos, imgPopUpLanScrAvisoBckGND, imgPopUpLanScrAvisoNivelAguaBajo, imgPopUpLanScrAvisoNivelAguaMedio, imgPopUpLanScrAvisoTempAmb, imgPopUpLanScrAvisoRFID, imgPopUpLanScrAvisoRTC, imgPopUpLanScrAvisoFiltroAire, //Pantalla de SAT imgFondoSATscreen, butHomeSATscreen, numVwrPaginaSATscreen, butNextPagSATscreen, butPrevPagSATscreen, imgVwr1, imgVwr2, imgVwr3, imgVwr4, imgVwr5, imgVwr6, imgVwr7, imgVwr8, imgVwr9, imgVwr10_1, imgVwr10_2, imgVwr11_1, imgVwr11_2, imgVwr12_1, imgVwr12_2, imgVwr13_1, imgVwr13_2, imgVwr14_1, imgVwr14_2, imgVwr15_1, imgVwr15_2, imgVwr16_1, imgVwr16_2, imgVwr17_1, imgVwr17_2, imgVwr18_1, imgVwr18_2, imgVwr19_1, imgVwr19_2, imgVwr20_1, imgVwr20_2, imgVwr21_1, imgVwr21_2, imgVwr22_1, imgVwr22_2, imgVwr23_1, imgVwr23_2, imgVwr24_1, imgVwr24_2, imgVwr25_1, imgVwr25_2, imgVwr26_1, imgVwr26_2, imgVwr27_1, imgVwr27_2, imgVwr28_1, imgVwr28_2, numVwr1, numVwr2, numVwr3, numVwr4, numVwr5, numVwr6, numVwr7, numVwr8, numVwr9, numVwr10, numVwr11, numVwr12, numVwr13, numVwr14, numVwr15, numVwr16, numVwr17, numVwr18, numVwr19, numVwr20, numVwr21, numVwr22, numVwr23, numVwr24, numVwr25, numVwr26, numVwr27, numVwr28, imgVacuum, imgFrio, imgRFID, imgTestLed, imgTestLedStatus, imgTestLedRGD, swVacuum, swFrio, swRFID, swTestLed, swTestLedRGB, PopupErrorSAT, imgVwrFondoErrorSAT, numVwrError1, numVwrError2, numVwrError3, numVwrError4, numVwrError5, butOkerr, //Pantalla de FATIGA imgFondoFATIGAscreen, butHomeFATIGAscreen, imgStep1, imgStep1status, imgStep2, imgStep2status, imgStep3, imgStep3status, imgStep4, imgStep4statusHP1, imgStep4statusHP2, imgStep4statusHP3, imgStep4statusHP4, imgStep5, imgStep5statusHP1, imgStep5statusHP2, imgStep5statusHP3, imgStep5statusHP4, imgStep6, imgStep6status, imgStep7, imgStep7status, numVwrTempAmb, numVwrTempBomba, numVwrTempRetorno, numVwrCaudalRetorno, numVwrVtobera, numVwrRth, numVwrCPI, numVwrCaudalRetornoHP1, numVwrCaudalRetornoHP2, numVwrCaudalRetornoHP3, numVwrCaudalRetornoHP4, numVwrVacuumHP1, numVwrVacuumHP2, numVwrVacuumHP3, numVwrVacuumHP4, numVwrFatigaTime, numVwrErrFatiga1, numVwrErrFatiga2, numVwrErrFatiga3, numVwrErrFatiga4, numVwrErrFatiga5, imgVwrVAC1, imgVwrVAC2, imgVwrVAC3, imgVwrVAC4, imgVwrCaudal1, imgVwrCaudal2, imgVwrCaudal3, imgVwrCaudal4, imgVwrTempAmb, imgVwrTempBomba, imgVwrTempRetorno, imgVwrCaudalRetorno, imgVwrVtobera, imgVwrRth, imgVwrCPI, //447 swFatigueFailBeep, APP_GUI_NUM_WIDGETS }cAppGUIwidgetIDlist; Q_ENUM_NS(EAppGUIwidgetIDlist) } using namespace AppEnumsNs; #endif /* APPGUIINTERFACESTRUCTURE_H_ */
AppGuiInterfaceStructure.cpp
/*Includes*/ #include "AppGuiInterfaceStructure.h"
main.cpp
#include <QGuiApplication> #include <QQmlApplicationEngine> #include <QQmlEngine> #include "app_environment.h" #include "import_qml_plugins.h" #include "AppGUI/AppGuiInterfaceStructure.h" int main(int argc, char *argv[]) { set_qt_environment(); QGuiApplication app(argc, argv); QQmlApplicationEngine engine; const QUrl url(u"qrc:Main/main.qml"_qs); QObject::connect( &engine, &QQmlApplicationEngine::objectCreated, &app, [url](QObject *obj, const QUrl &objUrl) { if (!obj && url == objUrl) QCoreApplication::exit(-1); }, Qt::QueuedConnection); engine.addImportPath(QCoreApplication::applicationDirPath() + "/qml"); engine.addImportPath(":/"); qmlRegisterUncreatableMetaObject(AppEnumsNs::staticMetaObject, "define.enums", 1, 0, "AppEnumsNs", "Access to enums only"); engine.load(url); if (engine.rootObjects().isEmpty()) { return -1; } return app.exec(); }
App.qml
import QtQuick import QtQuick.Window import EnumsProblem import define.enums 1.0 Window { width: mainScreen.width height: mainScreen.height visible: true title: "EnumsProblem" Screen01 { id: mainScreen button.onReleased: text1Text = AppEnumsNs.imgLoadAnim6 } }
Screen01.ui.qml
import QtQuick import QtQuick.Controls import EnumsProblem Rectangle { width: Constants.width height: Constants.height color: Constants.backgroundColor property alias text1Text: text1.text property alias button: button Text { id: text1 width: 196 height: 102 text: qsTr("Hello EnumsProblem") horizontalAlignment: Text.AlignHCenter verticalAlignment: Text.AlignVCenter font.pointSize: 35 anchors.centerIn: parent font.family: Constants.font.family } Button { id: button x: 478 y: 467 text: qsTr("Button") } }
When I'm assigning value from the first enum, e.g. AppEnumsNs.APP_GUI_MAXNUM_HP, it works fine and the value is displayed.
If it's from the second, e.g. AppEnumsNs.imgLoadAnim6, I get an error during execution: Error: Cannot assign [undefined] to QString -
TypeError: Cannot access enum value 'imgLoadAnim6' of 'AppEnumsNs', enum values need to start with an uppercase letter.
Next time you post code make sure it is runnable:
- Create a new test project.
- Add code to project until you can reproduce the issue.
- Make sure it can run.
- Then post that project.
Posting stuff from existing projects makes it harder to help you.
Test in 5.15.2 btw.