Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Error while setting QQmlApplicationEngine ->rootContext()->setContextProperty
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 847 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Slash200
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • dheerendraD Offline
        dheerendraD Offline
        dheerendra
        Qt Champions 2022
        wrote on last edited by
        #3

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

        Dheerendra
        @Community Service
        Certified Qt Specialist
        http://www.pthinks.com

        S 1 Reply Last reply
        0
        • dheerendraD dheerendra

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

          S Offline
          S Offline
          Slash200
          wrote on last edited by Slash200
          #4

          @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
          
          
          1 Reply Last reply
          0
          • dheerendraD Offline
            dheerendraD Offline
            dheerendra
            Qt Champions 2022
            wrote on last edited by
            #5

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

            Dheerendra
            @Community Service
            Certified Qt Specialist
            http://www.pthinks.com

            1 Reply Last reply
            1
            • S Offline
              S Offline
              Slash200
              wrote on last edited by Slash200
              #6

              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

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved