Communicating a Qml file with a C++ file in Qt
-
Hello, I'm making a project and I'm trying to communicate the QML side and the C++ side in my project.
In order not to create an object on the QML side I am using qmlRegisterUncreableType but not working i get error// person.h #ifndef PERSON_H #define PERSON_H #include <QObject> class Person : public QObject { Q_OBJECT public: explicit Person(QObject *parent = nullptr); virtual ~Person(); Q_PROPERTY(QString name READ name NOTIFY nameChanged) QString name() const; Q_PROPERTY(int age READ age NOTIFY ageChanged) int age() const; signals: void nameChanged(const QString &name); void ageChanged(int age); public slots: void setName(const QString &name); void setAge(int age); private: QString m_name; int m_age; }; #endif // PERSON_H// person.cpp #include "person.h" Person::Person(QObject *parent) : QObject(parent) { } Person::~Person() { } QString Person::name() const { return m_name; } int Person::age() const { return m_age; } void Person::setName(const QString &name) { if (m_name == name) return; m_name = name; emit nameChanged(m_name); } void Person::setAge(int age) { if (m_age == age) return; m_age = age; emit ageChanged(m_age); }#include <QGuiApplication> #include <QQmlApplicationEngine> #include <QQmlContext> #include "person.h" int main(int argc, char *argv[]) { QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QGuiApplication app(argc, argv); qmlRegisterUncreatableType<Person>("People", 1, 0, "Person", "Can not create Person in QML"); QQmlApplicationEngine engine; engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); if (engine.rootObjects().isEmpty()) return -1; return app.exec(); }// main.qml import QtQuick 2.0 import People 1.0 Rectangle { width: 200 height: 200 Text { text: Person.name anchors.centerIn: parent } }error: qrc:/main.qml:10:9: Unable to assign [undefined] to QString
-
S serkan_tr has marked this topic as solved on