QMetaProperty::read: 'Unable to handle unregistered datatype' error



  • Whats the misstake? Whats missing?
    Actually we try to have a class with text we use in more projects so we have seperate translation .ts files for all of them. So we have to translate it just once.
    We add a "Translate" class. In this class we have for example a other class with some material names.
    Now we want to use them in cpp and qml.
    On execute the project we got the "Unable to handle unregistered datatype" error! But we don't no why!

    Here is a extract of our code!
    If someone has a hint, it would be very helpful

    Translate.h

    namespace translationids
    {
    class Translate : public QObject
    {
      Q_OBJECT
    
    public:
      static TranslateMaterial TranslateMaterials;
      Q_PROPERTY(TranslateMaterial *translateMaterials READ getTranslateMaterials() CONSTANT)
    
      //% "Menu"
      static constexpr const char *common_MainMenu = QT_TRID_NOOP("common.mainmenu");
    
      /** enables attached properties in QML */
      static Translate *qmlAttachedProperties(QObject *);
    
    private:
      /** Default constructor */
      Translate(QObject *parent = 0);
    
      /** destructor */
      virtual ~Translate() {}
    };
    
    } /* namespace translationids */
    
    QML_DECLARE_TYPEINFO(translationids::Translate, QML_HAS_ATTACHED_PROPERTIES)
    
    

    Translate.cpp

    #include "Translate.h"
    #include "TranslateMaterial.h"
    
    using namespace translationids;
    
    TranslateMaterial Translate::translateMaterials;
    
    Translate *Translate::qmlAttachedProperties(QObject *object)
    {
      return new Translate(object);
    }
    
    Translate::Translate(QObject *parent)
      : QObject(parent)
    {
    }
    
    TranslateMaterial* Translate::getTranslateMaterials()
    {
      return &translateMaterials;
    }
    

    TranslateMaterial.h

          namespace translationids
          {
            /** class Material Translations *********/
            class TranslateMaterial
            {
                Q_GADGET
    
              public:
                //% "User defined"
                static constexpr const char* material_UserDefined = QT_TRID_NOOP("material.userdefined");
                Q_PROPERTY(QString material_UserDefined MEMBER _Material_UserDefined CONSTANT)
                //% "Aluminium"
                static constexpr const char* material_Aluminium = QT_TRID_NOOP("material.aluminium");
                Q_PROPERTY(QString material_Aluminium MEMBER _Material_Aluminium CONSTANT)
                //% "Wood"
                static constexpr const char* material_Wood = QT_TRID_NOOP("material.wood");
                Q_PROPERTY(QString material_Wood MEMBER _Material_Wood CONSTANT)
    
               
                TranslateMaterial();
    
                ~TranslateMaterial() {}
    
            };
          } /* namespace translationids */
    
    Q_DECLARE_METATYPE(translationids::TranslateMaterial)
    

    TranslateMaterial.cpp

    using namespace translationids;
    
    TranslateMaterial::TranslateMaterial()
    {
    }
    

    Main.cpp

     qmlRegisterUncreatableType<translationids::Translate>("translationids.translate", 0, 1, "Translate", "translate only has attached properties! You can not create an instance.");
     qmlRegisterUncreatableType<translationids::TranslateMaterial>("translationids.translate", 0, 1, "TranslateMaterial", "TranslateMaterial is a Q_GADGET! You can not create an instance.");
    

    Using in cpp

    #include <translationIDs/Translate.h>
    
    std::vector<Material::MaterialStruct> Material::ms_vecMaterial =
    {
      // Material Name, String ID
      { "UserDefined", Translate::TranslateMaterials.material_UserDefined },
      { "Aluminium", Translate::TranslateMaterials.material_Aluminium },
      { "Wood", Translate::TranslateMaterials.material_Wood },
    }
    

    Using in qml

    import translationids.translate 0.1
         
         Text{
            id:headerText
            anchors.centerIn: parent
            height: parent.height
            width: parent.width
            text: qsTrId(Translate.translateMaterials.material_Aluminium) 
          }
    

    Thats are the Error Messages:
    QMetaProperty::read: Unable to handle unregistered datatype 'TranslateMaterial*' for property 'translationids::Translate::translateMaterials'
    MaterialTopDownMenu.qml:72 ((null)): MaterialTopDownMenu.qml:72: TypeError: Cannot read property 'material_Aluminium' of undefined

    What is missing?



  • It's difficult to answer if you don't provide compilable code (std::vectorQString::Material ???).



  • Sorry that was a misstake,

    std::vector<Material::MaterialStruct> Material::ms_vecMaterial =
    

    But it doesn't matter because the cpp works. I just got the problem with qml!



  • Where and how is Translate instantiated? (as I said, it would be much easier to answer if you should provide a complete example).



  • I'm sorry but it's hard to seperate this things from project!
    Maybe this helps you a litte bit more:

    If i write

     text: qsTrId(TlibTranslate.tlib_Common_MainMenu)
    

    it works!

    But

    text: qsTrId(TlibTranslate.tlibTranslateMaterials.tlib_Material_Aluminium)
    

    doesn't


  • Lifetime Qt Champion

    Hi,

    Since you are using your custom type in a property you should also take a look at the Custom Types chapter in Qt's documentation.

    Basically you are not making the type know to the Qt meta system. See Q_DECLARE_METATYPE and qRegisterMetaType.



  • They are set!

    Translate.h

    QML_DECLARE_TYPEINFO(translationids::Translate, QML_HAS_ATTACHED_PROPERTIES)
    

    TranslateMaterial.h

    Q_DECLARE_METATYPE(translationids::TranslateMaterial)
    

    Main.cpp

    qmlRegisterUncreatableType<translationids::Translate>("translationids.translate", 0, 1, "Translate", "translate only has attached properties! You can not create an instance.");
     qmlRegisterUncreatableType<translationids::TranslateMaterial>("translationids.translate", 0, 1, "TranslateMaterial", "TranslateMaterial is a Q_GADGET! You can not create an instance.");
    


  • Okay, ifound it out by myself!
    Everything works well if i define the TranslateMaterial class as a Q_OBJECT, like the Translate class!


  • Lifetime Qt Champion

    My bad, the code parts can be tricky to read when scrolling is involved.

    Glad you found a solution hand thanks for sharing.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.