Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. How to get valid class instance after doing a qmlRegisterType?
Forum Updated to NodeBB v4.3 + New Features

How to get valid class instance after doing a qmlRegisterType?

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
18 Posts 6 Posters 13.8k Views 4 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.
  • ? A Former User

    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();
    }
    
    N Offline
    N Offline
    Nelson_Piquet
    wrote on last edited by Nelson_Piquet
    #8

    @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();
    }

    ? 1 Reply Last reply
    0
    • N Nelson_Piquet

      @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();
      }

      ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #9

      @Nelson_Piquet Yes, that's totally fine.

      1 Reply Last reply
      0
      • p3c0P Offline
        p3c0P Offline
        p3c0
        Moderators
        wrote on last edited by
        #10

        @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.

        157

        1 Reply Last reply
        0
        • N Nelson_Piquet

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

          J Offline
          J Offline
          JosephMills
          wrote on last edited by JosephMills
          #11

          @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();
          }
          
          
          kshegunovK 1 Reply Last reply
          0
          • J JosephMills

            @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();
            }
            
            
            kshegunovK Offline
            kshegunovK Offline
            kshegunov
            Moderators
            wrote on last edited by
            #12

            @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
            }
            

            Read and abide by the Qt Code of Conduct

            J 1 Reply Last reply
            0
            • kshegunovK kshegunov

              @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
              }
              
              J Offline
              J Offline
              JosephMills
              wrote on last edited by JosephMills
              #13

              @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.

              kshegunovK N 2 Replies Last reply
              0
              • J JosephMills

                @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.

                kshegunovK Offline
                kshegunovK Offline
                kshegunov
                Moderators
                wrote on last edited by
                #14

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

                Read and abide by the Qt Code of Conduct

                1 Reply Last reply
                0
                • J JosephMills

                  @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.

                  N Offline
                  N Offline
                  Nelson_Piquet
                  wrote on last edited by
                  #15

                  @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 ?

                  J 1 Reply Last reply
                  0
                  • N Nelson_Piquet

                    @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 ?

                    J Offline
                    J Offline
                    JosephMills
                    wrote on last edited by JosephMills
                    #16

                    @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

                    1 Reply Last reply
                    0
                    • N Offline
                      N Offline
                      Nelson_Piquet
                      wrote on last edited by Nelson_Piquet
                      #17

                      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.

                      1 Reply Last reply
                      0
                      • S Offline
                        S Offline
                        Shanna951
                        Banned
                        wrote on last edited by
                        #18
                        This post is deleted!
                        1 Reply Last reply
                        0

                        • Login

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