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. How to use qmlRegisterUncreableType
Forum Updated to NodeBB v4.3 + New Features

How to use qmlRegisterUncreableType

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 1.3k Views 2 Watching
  • 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.
  • serkan_trS Offline
    serkan_trS Offline
    serkan_tr
    wrote on last edited by
    #1

    Not many resources on how to use qmlRegisterUncreableType. i tried to use it but i get errors

    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    #include <QQmlContext>
    #include "comm.h"
    
    /*
     *  qmlRegisterType
     *  qmlRegisterUncreatableType
    */
    int main(int argc, char *argv[])
    {
        comm* com_port = new comm();
        com_port->settx_data(10);
        com_port->setname_classs("serkan");
        QGuiApplication app(argc, argv);
        QQmlApplicationEngine engine;
        const QUrl url(QStringLiteral("qrc:/main.qml"));
        QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                         &app, [url](QObject *obj, const QUrl &objUrl) {
            if (!obj && url == objUrl)
                QCoreApplication::exit(-1);
        }, Qt::QueuedConnection);
    
        qmlRegisterUncreatableType<comm>("com.example", 1, 0, "comm","MyCustomClass should not be created from QML.");
    
    //    engine.rootContext()->setContextProperty("myObject", com_port);
        engine.load(url);
        app.exec();
        delete com_port;
        return 0 ;
    }
    
    
    import QtQuick 2.15
    import QtQuick.Window 2.15
    import com.example 1.0
    
    Window {
        width: 640
        height: 480
        visible: true
        title: qsTr("Hello World")
        Rectangle{
            height: 100
            width:  100
            anchors.centerIn: parent
            color:"black"
            Text {
                id: aaa
                text: comm.name_classs
                anchors.centerIn: parent
                color:"green"
            }
        }
    }
    
    
    #include "comm.h"
    
    comm::comm(): value(0), name("")
    {
        qDebug() << "Constructor Worked...";
    
    }
    comm::~comm(){
        qDebug() << "Destructor Worked...";
    }
    
    QString comm::getName()
    {
        qDebug() << name;
        return name;
    
    }
    
    void comm::setName(QString newName)
    {
        if(name == newName){
            name = newName;
        }
    }
    
    int comm::tx_data() const
    {
        return m_tx_data;
    }
    
    void comm::settx_data(int newTx_data)
    {
        if (m_tx_data == newTx_data)
            return;
        m_tx_data = newTx_data;
        emit tx_dataChanged();
    }
    
    QString comm::name_classs() const
    {
        return m_name_classs;
    }
    
    void comm::setname_classs(const QString &newName_classs)
    {
        if (m_name_classs == newName_classs)
            return;
        m_name_classs = newName_classs;
        emit name_classsChanged();
    }
    
    
    #ifndef COMM_H
    #define COMM_H
    
    #include <QObject>
    #include <QDebug>
    
    class comm: public QObject
    {
        Q_OBJECT
        Q_PROPERTY(int tx_data READ tx_data WRITE setTx_data NOTIFY tx_dataChanged)
        Q_PROPERTY(QString name_classs READ name_classs WRITE setName_classs NOTIFY name_classsChanged)
    public:
        comm();
        ~comm();
        QString getName();
        void setName(QString newName);
    
        int tx_data() const;
        void settx_data(int newTx_data);
    
        QString name_classs() const;
        void setname_classs(const QString &newName_classs);
    
    signals:
        void tx_dataChanged();
    
        void name_classsChanged();
    
    private:
        QString name;
        int value;
        int m_tx_data;
        QString m_name_classs;
    };
    
    #endif // COMM_H
    
    
    sierdzioS 1 Reply Last reply
    0
    • serkan_trS serkan_tr

      Not many resources on how to use qmlRegisterUncreableType. i tried to use it but i get errors

      #include <QGuiApplication>
      #include <QQmlApplicationEngine>
      #include <QQmlContext>
      #include "comm.h"
      
      /*
       *  qmlRegisterType
       *  qmlRegisterUncreatableType
      */
      int main(int argc, char *argv[])
      {
          comm* com_port = new comm();
          com_port->settx_data(10);
          com_port->setname_classs("serkan");
          QGuiApplication app(argc, argv);
          QQmlApplicationEngine engine;
          const QUrl url(QStringLiteral("qrc:/main.qml"));
          QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                           &app, [url](QObject *obj, const QUrl &objUrl) {
              if (!obj && url == objUrl)
                  QCoreApplication::exit(-1);
          }, Qt::QueuedConnection);
      
          qmlRegisterUncreatableType<comm>("com.example", 1, 0, "comm","MyCustomClass should not be created from QML.");
      
      //    engine.rootContext()->setContextProperty("myObject", com_port);
          engine.load(url);
          app.exec();
          delete com_port;
          return 0 ;
      }
      
      
      import QtQuick 2.15
      import QtQuick.Window 2.15
      import com.example 1.0
      
      Window {
          width: 640
          height: 480
          visible: true
          title: qsTr("Hello World")
          Rectangle{
              height: 100
              width:  100
              anchors.centerIn: parent
              color:"black"
              Text {
                  id: aaa
                  text: comm.name_classs
                  anchors.centerIn: parent
                  color:"green"
              }
          }
      }
      
      
      #include "comm.h"
      
      comm::comm(): value(0), name("")
      {
          qDebug() << "Constructor Worked...";
      
      }
      comm::~comm(){
          qDebug() << "Destructor Worked...";
      }
      
      QString comm::getName()
      {
          qDebug() << name;
          return name;
      
      }
      
      void comm::setName(QString newName)
      {
          if(name == newName){
              name = newName;
          }
      }
      
      int comm::tx_data() const
      {
          return m_tx_data;
      }
      
      void comm::settx_data(int newTx_data)
      {
          if (m_tx_data == newTx_data)
              return;
          m_tx_data = newTx_data;
          emit tx_dataChanged();
      }
      
      QString comm::name_classs() const
      {
          return m_name_classs;
      }
      
      void comm::setname_classs(const QString &newName_classs)
      {
          if (m_name_classs == newName_classs)
              return;
          m_name_classs = newName_classs;
          emit name_classsChanged();
      }
      
      
      #ifndef COMM_H
      #define COMM_H
      
      #include <QObject>
      #include <QDebug>
      
      class comm: public QObject
      {
          Q_OBJECT
          Q_PROPERTY(int tx_data READ tx_data WRITE setTx_data NOTIFY tx_dataChanged)
          Q_PROPERTY(QString name_classs READ name_classs WRITE setName_classs NOTIFY name_classsChanged)
      public:
          comm();
          ~comm();
          QString getName();
          void setName(QString newName);
      
          int tx_data() const;
          void settx_data(int newTx_data);
      
          QString name_classs() const;
          void setname_classs(const QString &newName_classs);
      
      signals:
          void tx_dataChanged();
      
          void name_classsChanged();
      
      private:
          QString name;
          int value;
          int m_tx_data;
          QString m_name_classs;
      };
      
      #endif // COMM_H
      
      
      sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by sierdzio
      #2

      You have to register before you construct the QML engine QQmlApplicationEngine engine;.

      Also, your comm constructor should call parent class constructor:

      comm::comm(): QObject(nullptr), value(0), name("")
      

      Lastly, you try to use comm in QML as if it was an object, but it is uncreatable. There is no instance, so you can't call name_classs property. There are 2 reasons to use qmlRegisterUncreableType:

      • you need to use enum values declared in C++ on QML side
      • your QML code needs to access instances of registered type but should not be allowed to construct them

      (Z(:^

      serkan_trS 1 Reply Last reply
      2
      • sierdzioS sierdzio

        You have to register before you construct the QML engine QQmlApplicationEngine engine;.

        Also, your comm constructor should call parent class constructor:

        comm::comm(): QObject(nullptr), value(0), name("")
        

        Lastly, you try to use comm in QML as if it was an object, but it is uncreatable. There is no instance, so you can't call name_classs property. There are 2 reasons to use qmlRegisterUncreableType:

        • you need to use enum values declared in C++ on QML side
        • your QML code needs to access instances of registered type but should not be allowed to construct them
        serkan_trS Offline
        serkan_trS Offline
        serkan_tr
        wrote on last edited by
        #3

        @sierdzio
        Thank you

        1 Reply Last reply
        0
        • serkan_trS serkan_tr has marked this topic as solved on

        • Login

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