How to get valid class instance after doing a qmlRegisterType?



  • I am using Qt 5.7 on ios & android. I use call to qmlRegisterType to instantiate MyClass derived from QQuickItem view. Here is my code which instantiates MyClass in QMLEngine.

    int main(int argc, char *argv[])
    {
        QGuiApplication app(argc, argv);
        qmlRegisterType<MyClass>("MyClass", 1, 0, "MyClass");
        QQmlApplicationEngine engine;
        QQmlContext* ctx = engine.rootContext();
        engine.load(QUrl(QStringLiteral("qrc:/qml/main.qml")));
        return app.exec();
    }
    

    How can I get a valid object of MyClass back from qml ? Is it possible to get it from the QQmlContext instance ?


  • Moderators

    Hi!

    @Nelson_Piquet said in How to get valid class instance after doing a qmlRegisterType?:

    I use call to qmlRegisterType to instantiate MyClass derived from QQuickItem view.

    Maybe I misunderstood you here but calling qmlRegisterType alone doesn't instantiate anything.



  • @Wieland Is there some other way I can instantiate MyClass & set it to qml ?



  • @Wieland I am new to Qt. So I could be wrong in saying that qmlRegisterType instantiates anything


  • Moderators

    @Nelson_Piquet How do you plan to use MyClass? Do you want to create instances from it in QML or do you only need a single MyClass object that is used from C++ and QML?



  • @Wieland I need a single MyClass object that is used from C++ and QML. My complete application will have only one instance of MyClass.


  • Moderators

    Ok. Let's say this is MyClass:

    #ifndef MYCLASS_H
    #define MYCLASS_H
    
    #include <QObject>
    #include <QString>
    
    class MyClass : public QObject
    {
        Q_OBJECT
    public:
        explicit MyClass(QObject *parent = 0);
        Q_INVOKABLE QString sayHello() const;
    };
    
    #endif // MYCLASS_H
    
    #include "myclass.h"
    
    MyClass::MyClass(QObject *parent) 
        : QObject(parent)
    {
    }
    
    QString MyClass::sayHello() const
    {
        return "Hello!";
    }
    

    And this is main.qml:

    import QtQuick 2.7
    import QtQuick.Controls 2.0
    import QtQuick.Layouts 1.0
    
    import com.mycompany.myapplication 1.0
    
    ApplicationWindow {
        visible: true
        width: 640
        height: 480
        title: qsTr("Hello World")
    
        Text {
            anchors.centerIn: parent
            text: myClass.sayHello()
        }
    }
    

    In main.cpp just do this:

    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    #include <QQmlContext>
    #include <QQmlEngine>
    
    #include "myclass.h"
    
    int main(int argc, char *argv[])
    {
        QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
        QGuiApplication app(argc, argv);
        qmlRegisterType<MyClass>("com.mycompany.myapplication", 1, 0, "MyClass");
        QQmlApplicationEngine engine;
        MyClass myClass;
        engine.rootContext()->setContextProperty("myClass", &myClass);
        engine.load(QUrl(QLatin1String("qrc:/main.qml")));
        return app.exec();
    }
    


  • @Wieland Thanks for such a vivid reply. if I have a method in MyClass where I want to set some data, then is following the correct point to set it ?

    int main(int argc, char *argv[])
    {
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QGuiApplication app(argc, argv);
    qmlRegisterType<MyClass>("com.mycompany.myapplication", 1, 0, "MyClass");
    QQmlApplicationEngine engine;
    MyClass myClass;
    myClass.setProperty("something"): //Is this the correct place to set it ?
    engine.rootContext()->setContextProperty("myClass", &myClass);
    engine.load(QUrl(QLatin1String("qrc:/main.qml")));
    return app.exec();
    }


  • Moderators

    @Nelson_Piquet Yes, that's totally fine.


  • Moderators

    @Nelson_Piquet To add to @Wieland's answer you don't have to register MyClass with qmlRegisterType as you already have access to MyClass in QML as a context property. Ofcourse unless you don't want to instantiate it as a QML component.



  • @Wieland I need a single MyClass object that is used from C++ and QML. My complete application will have only one instance of MyClass.

    I would use qmlRegisterSingletonType and just register that once and that way it is only created once on your application.

    for more on this and instantiation of this see docs here Here

    for future if someone runs across this it is in the qqmlengine docs as of 5.8

    As a side note .... This makes not sense to me at all.

    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    #include <QQmlContext>
    #include <QQmlEngine>
    
    #include "myclass.h"
    int main(int argc, char *argv[])
    {
     
       // At this point you are passing a object that makes a instance of your class
       // To be used in QML via qmlRegisterType 
       
      //  Example :
      //   MyClass{id: myclass }   
      //   Text{text: myclass.sayHello();}
      
       qmlRegisterType<MyClass>("com.mycompany.myapplication", 1, 0, "MyClass");
    
    
       // But than You are doing the same thing again just using up more memory.
       // IE this is not needed at all 
       MyClass myClass;
       engine.rootContext()->setContextProperty("myClass", &myClass);
    }
    

    So this is how I would do this

    #ifndef MYCLASS_H
    #define MYCLASS_H
    
    #include <QObject>
    #include <QString>
    
    class MyClass : public QObject
    {
        Q_OBJECT
    public:
        explicit MyClass(QObject *parent = 0);
        Q_INVOKABLE QString sayHello() const;
    };
    
    #endif // MYCLASS_H
    #include "myclass.h"
    
    MyClass::MyClass(QObject *parent) 
        : QObject(parent)
    {
    }
    
    QString MyClass::sayHello() const
    {
        return "Hello!";
    }
    
    

    In Qml

    import QtQuick 2.7
    import QtQuick.Controls 2.0
    import QtQuick.Layouts 1.0
    
    import com.mycompany.myapplication 1.0
    
    ApplicationWindow {
        visible: true
        width: 640
        height: 480
    
        Text {
            anchors.centerIn: parent
            text: MyClass.sayHello()
        }
    }
    
    

    Wrap it all up as a single instance.

    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    #include <QQmlContext>
    #include <QQmlEngine>
    
    #include "myclass.h"
    
    static QObject *mySingleObject(QQmlEngine *engine, QJSEngine *scriptEngine)
    {
        Q_UNUSED(engine)
        Q_UNUSED(scriptEngine)
    
        MyClass *myClass = new MyClass();
        return myClass;
    }
    
    int main(int argc, char *argv[])
    {
        QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
        QGuiApplication app(argc, argv);
        qmlRegisterSingletonType<MyClass>("com.mycompany.myapplication", 1, 0, "MyClass" , mySingleObject);
        QQmlApplicationEngine engine;
        engine.load(QUrl(QLatin1String("qrc:/main.qml")));
        return app.exec();
    }
    
    

  • Qt Champions 2016

    @JosephMills said in How to get valid class instance after doing a qmlRegisterType?:

      // At this point you are passing a object that makes a instance of your class
      // To be used in QML via qmlRegisterType 
       
      //  Example :
      //   MyClass{id: myclass }   
      //   Text{text: myclass.sayHello();}
      
       qmlRegisterType<MyClass>("com.mycompany.myapplication", 1, 0, "MyClass");
    

    You're wrong. qmlRegisterType registers the type (i.e. class) in the QML engine. It creates no objects by itself. It makes it possible that you're able to instantiate objects of that type:

    import com.mycompany.myapplication 1.0
    
    MyClass {    //< This creates an object (instance of MyClass) and consumes memory
    }
    


  • @kshegunov That is exactly what I said

    Not sure how to make quote on the forum yet but ,

      // At this point you are passing a object that makes a instance of your class
      // To be used in QML via qmlRegisterType 
       
      //  ***Example:***
      //   MyClass{id: myclass }   
    

    Pardon if I was not clear enough. I will try to be more clear.


  • Qt Champions 2016

    Nope, it's my fault, sorry. I need more coffee, as I am apparently still sleeping.



  • @JosephMills @Wieland @kshegunov I am trying around the suggestions. This is extremely good info around my question.

    I'd said in my question that MyClass is derived from QQuickItem Looks as below

    class MyClass : public QQuickItem
    {
    Q_OBJECT
    public:
    MyClass();
    virtual ~ MyClass();
    signals:
    void visibilityChanged(bool visibility);
    public Q_SLOTS:
    void handleVisibilityChanged(bool visibility);
    };

    Do you guys mean that I should derive it from QObject instead of QQuickItem ? Or do solutions you guys provide work with QQuickItem derived classes as well ?



  • @Nelson_Piquet If you are going to make a Object or a Item I guess that is the question.

    Example do you need the polymorphism of QQuickItem ? Like anchors ,opacity , x and y and all that that comes with QQuickItem. Or Do you need it as just a QObject ? This really depends on how you are designing the class that you are writing. Please note that QQuickItem Inherits: QObject and QQmlParserStatus. So maybe to move forward you can tell us a little bit more about the class that you are trying to expose to QML.

    Things like is it all methods ? or only used to gather info ? Does it paint anything or take in other QQuickItems as Q_PROPERTY ect. Please also note that QQuickItem has visible that you have access to already.

    Example:

    void handleVisibilityChanged(const bool &visibility)
    {
             if(isVisible() )
             {
                        setVisible(false);
             }
             else 
             {
                        setVisible(true);
             }  
    }
    

    As you can see I made it a const and also made it so that it is dereference. I am also wondering about your signal and why there is a value in it.

    Cheers,
    Joseph Mills



  • I really need my class to be derived from QQuickItem as I use the anchors ,opacity , x, y & much more. Its required to be displayed specifically on a wide range of mobile devices. This class is basically used to paint video data on to the screen with some open gl processing. It has a lot of methods & code to support that. All I need is the access to the object that in my main.cpp. The suggestions are really great but would have been best if the samples provided were with a class derived from QQuickItem. I am going to try out your suggestions anyways keeping MyClass derived from QQuickItem.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.