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. Communicating a Qml file with a C++ file in Qt
Forum Updated to NodeBB v4.3 + New Features

Communicating a Qml file with a C++ file in Qt

Scheduled Pinned Locked Moved Solved General and Desktop
1 Posts 1 Posters 149 Views 1 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

    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

    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