How to use qmlRegisterUncreableType
-
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
-
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
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 callname_classs
property. There are 2 reasons to useqmlRegisterUncreableType
:- 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
- you need to use
-
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 callname_classs
property. There are 2 reasons to useqmlRegisterUncreableType
:- 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
- you need to use
-
S serkan_tr has marked this topic as solved on