Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Error while setting QQmlApplicationEngine ->rootContext()->setContextProperty



  • Hello,

    I'm setting a ContextProperty for a QQmlApplicationEngine and getting a this error:

    C:\Qt\5.10.0\mingw53_32\include\QtCore\qmetatype.h:1377: Fehler: 'QObject' is an inaccessible base of 'DataSourceObject'
             enum { Value = sizeof(checkType(static_cast<T*>(Q_NULLPTR))) == sizeof(yes_type) };
    
    
    C:\Qt\5.10.0\mingw53_32\include\QtCore\qglobal.h:756: Fehler: static assertion failed: Type is not registered, please use the Q_DECLARE_METATYPE macro to make it known to Qt's meta-object system
     #define Q_STATIC_ASSERT_X(Condition, Message) static_assert(bool(Condition), Message)
    

    My Code:

    #include "connect.h"
    #include "updreceiver.h"
    #include "datasourceobject.h"
    #include <QDebug>
    #include <QQmlContext>
    #include <QQmlApplicationEngine>
    
    
    Connect::Connect(QObject *parent) : QObject(parent)
    {
        //Create connection to QML Application
        QQmlApplicationEngine *engine = dynamic_cast<QQmlApplicationEngine*>( parent );
        if (engine == Q_NULLPTR) return;
    
         m_UpdReceiver = new UpdReceiver();
        //this Object contains all datasources as Object! Definition in datasourceobject.h!
        auto m_datasourcelist = m_UpdReceiver->initDataSources();
    
    
        //Link m_datasourcelist to Model on QML side
        engine->rootContext()->setContextProperty("dataSourceModel", QVariant::fromValue(m_datasourcelist));
    
        //m_UpdReceiver starts now to listen to incoming UDP packages
        m_UpdReceiver->startUdpReceiver();
    
    }
    

    m_datasourcelist is from type QList<DataSourceObject*>

    DataSourceObject is a class which should store values for a Datamodel for QML.

    regards
    Bastian


  • Lifetime Qt Champion

    Hi,

    As the error suggest you have to register your custom type.


  • Qt Champions 2017

    Inaddition to @SGaist, DataSourceObject is it inherited from QObject ? Also how did you declare QList ? Can you show the declaration ?



  • @dheerendra
    this is the declaration of the list:

    QList<DataSourceObject*> m_DataSourceList;
    

    And this the header of the DataSourceObject:

    #ifndef DATASOURCEOBJECT_H
    #define DATASOURCEOBJECT_H
    
    #include <QObject>
    
    class DataSourceObject : QObject
    {
        Q_OBJECT
    
        Q_PROPERTY(int id READ id WRITE setid NOTIFY idChanged)
        Q_PROPERTY(QString name READ name WRITE setname NOTIFY nameChanged)
        Q_PROPERTY(QString displayname READ displayname WRITE setdisplayname NOTIFY displaynameChanged)
    
    public:
    
        //Constructors and destructors
        DataSourceObject();
        DataSourceObject(int id, QString name, QString displayname){
            m_id = id;
            m_name = name;
            m_displayname = displayname;
        }
        DataSourceObject(const DataSourceObject&); //copy constructor
        ~DataSourceObject()=default;
    
        void setid(int id){
            m_id = id;
        }
    
        int id() const{
            return m_id;
        }
    
        void setname(QString name){
            m_name = name;
        }
    
        QString name() const{
            return m_name;
        }
    
        void setdisplayname(QString displayname){
            m_displayname = displayname;
        }
    
        QString displayname() const{
            return m_displayname;
        }
    
    signals:
    
        void idChanged(int id);
        void nameChanged(QString name);
        void displaynameChanged(QString displayname);
    
    private:
    
        int m_id;
        QString m_name;
        QString m_displayname;
    
    };
    
    Q_DECLARE_METATYPE(DataSourceObject);
    
    
    #endif // DATASOURCEOBJECT_H
    
    

  • Qt Champions 2017

    Have you registered the DataSourceObject using qmlRegisterMetaType(..) in main or somewhere ?



  • I found the problem.
    In the class definition I had

    class DataSourceObject : QObject
    

    instead of

    class DataSourceObject : public QObject
    

    Now registering with qmlRegisterType is working


Log in to reply