Is it possible to expose a C++ Class that doesn't have a default constructor to QML ?



  • Hello!

    The core of my problem is this:

    I want to use different instances of the same subclasses of QAbstractItemModel.
    To that end, I understand I need to register the class like so qmlRegisterType<MySubClass>("com.me.qmlcomponents", 1, 0, "MySubClass");

    However, my class doesn't have a default constructor because I need it to talk to a SQLite database so I send a pointer to a database manager class in the constructor, like so :

    BasicListModel::BasicListModel(DbManager *dbManager)
     {
      this->dbManager = dbManager;
      this->items = QList<QMap<QString, QVariant>>();
     }
    

    So this is my question, Is it possible to register a class that doesn't have a default constructor with QML ?

    Alternatively, is it possible to pass a pointer to the database manager class to an object instantiated by QML and registered with qmlRegisterType ?


  • Lifetime Qt Champion

    Hi,

    What exactly does that database manager do ?


  • Moderators

    @Curtwagner1984 said in Is it possible to expose a C++ Class that doesn't have a default constructor to QML ?:

    So this is my question, Is it possible to register a class that doesn't have a default constructor with QML ?

    No.

    You can, however, add an init() function and pass your pointer there, after QML engine creates the object.

    Or you can instantiate your object on C++ side (calling any constructor you like) and pass it to QML via context, or via some other object (returning BasicListModel pointer from some method).

    Alternatively, is it possible to pass a pointer to the database manager class to an object instantiated by QML and registered with qmlRegisterType ?

    Yes, and there are many ways to do it. Is your DbManager a QObject?



  • @Curtwagner1984 You can register classes without default ctor like:

    qmlRegisterUncreatableType<InputDeviceConfigurator>(
                    "InputDeviceConfigurator", 1, 0, "InputDeviceConfigurator", "Not creatable in Qml." );
    

    But then you can not create an instance of that class inside Qml via:

    InputDeviceConfigurator {
    id: myFatDevice
    }
    

    So, you have to pass a pointer to your model.
    You may also be interested in the little discussion here: https://forum.qt.io/topic/79725/how-to-use-q_disable_copy-class-without-ctor



  • @SGaist said in Is it possible to expose a C++ Class that doesn't have a default constructor to QML ?:

    Hi,

    What exactly does that database manager do ?

    It basically executes arbitrary SQL and parses the results into something my model can use. For example, I have a movie model, it has a search function. When the search function is called, it's calling the dbManager with a prepared SQL statement that returns a list of QMap<QString, QVariant> for each row of the search result where each value in the QMap corresponds to a column of the SQL table. I later use that list to implement the data() function of the model like so:

    QVariant MovieModel::data(const QModelIndex &index, int role) const
    {
        if (!index.isValid()){
                return QVariant();
            }
    
            QMap<QString,QVariant> currentItem = this->items.at(index.row());
    
            if (role == IdRole){
                return currentItem["id"];
            }else if (role == ThumbRole){
                return currentItem["thumbnail"];
            }else if (role == YearRole){
                return currentItem["year"];
            }else{
              return QVariant();
            }
    }
    

    @sierdzio said in Is it possible to expose a C++ Class that doesn't have a default constructor to QML ?:

    You can, however, add an init() function and pass your pointer there, after QML engine creates the object.

    How would I go about doing this? How would I know that the object was created in QML in order to trigger the init() function?
    I suppose I could connect it to a signal that is triggered by the default constructor, is that what you are suggesting?

    Or you can instantiate your object on C++ side (calling any constructor you like) and pass it to QML via context, or via some other object (returning BasicListModel pointer from some method).

    You mean something like exposing another object using setContextProperty that has a Q_INVOKABLE method along the lines of :

    BasicListModel* QmlComm::newListModel() {
      return new BasicListModel(this.dbManager);
    }
    

    and then in QML using it like this?

    ListView{
      id:myList
      model:qmlComm.newListModel();
    }
    

    Yes, and there are many ways to do it. Is your DbManager a QObject?

    Indeed it is.

    @c64zottel But the whole point of me doing this is that I want to be able to create different instances of the model from QML.



  • @Curtwagner1984 I just tried to pass the id as "this" pointer and it worked.

    So, you register all your derived classes as usual and use them with a proper id.
    The generic function takes a pointer of the base class, and you call that function with the id you issued in Qml.



  • @c64zottel said in Is it possible to expose a C++ Class that doesn't have a default constructor to QML ?:

    @Curtwagner1984 I just tried to pass the id as "this" pointer and it worked.

    So, you register all your derived classes as usual and use them with a proper id.
    The generic function takes a pointer of the base class, and you call that function with the id you issued in Qml.

    Can you please elaborate on what exactly you did? I'm not sure I follow. An example would be best.



  • @Curtwagner1984 Something like this:

    class Base : public QObject
    {
    ...
      Q_INVOKABLE virtual void func();
    
    signals:
      void activated();
    }
    
    class D1 : public Base
    {
        void func() {
            do stuff;
        }
    }
    
    class D2 : public Base
    {
        void func() {
            do other stuff;
        }
    }
    
    class SomeClientUsingBase : public QObject
    {
        Q_INVOKABLE void functionDealingWithSomethingDerivedFromBase( Base * b ) {
            b->func();  // uses either D1 or D2
        }
    }
    

    In Qml:

    qmlRegisterType<SomeClientUsingBase>("com.me.qmlcomponents", 1, 0, "Client");
    qmlRegisterType<Base>("com.me.qmlcomponents", 1, 0, "Base");
    qmlRegisterType<D1>("com.me.qmlcomponents", 1, 0, "D1");
    qmlRegisterType<D2>("com.me.qmlcomponents", 1, 0, "D2");
    
    Client {
        id: waitingToUseBase
    }
    
    D1 {
        id: d1
        onActivated: waitingToUseBase.functionDealingWithSomethingDerivedFromBase( d1 )
    }
    
    D2 {
        id: d2
        onActivated: waitingToUseBase.functionDealingWithSomethingDerivedFromBase( d1 )
    }
    

  • Moderators

    @sierdzio said in Is it possible to expose a C++ Class that doesn't have a default constructor to QML ?:

    You can, however, add an init() function and pass your pointer there, after QML engine creates the object.

    How would I go about doing this? How would I know that the object was created in QML in order to trigger the init() function?
    I suppose I could connect it to a signal that is triggered by the default constructor, is that what you are suggesting?

    Ugh, as always with QML, there are many ways to do it and I never know which solution to recommend :D Let's try this:

    // C++
    // add DbManager to QML context somewhere. Example:
    DbManager manager;
    QQmlApplicationEngine engine;
    engine.rootContext()->setContextProperty("Manager", &manager);
    [...]
    class BasicListModel
    {
      Q_INVOKABLE void init(DbManager *manager);
    }
    
    // QML
    [...]
    model: BasicListModel {
      Component.onCompleted: {
        init(manager);
      }
    }
    


  • This post is deleted!


  • @sierdzio Thank you! It works exactly as expected.

    Now I wonder, can I create an instance of BasicListModel dynamically? Something along the lines of :

    function generateDynamicInstance()
    {
        // returns a new instance of BasicListModel
    }
    
    Button {
          onClicked:{
            var newInstance = generateDynamicInstance();
            myListView.model = newInstance;
          }
      
    }
    
    ListView{
        id:myListView
    }
    

  • Moderators

    @Curtwagner1984 said in Is it possible to expose a C++ Class that doesn't have a default constructor to QML ?:

    Now I wonder, can I create an instance of BasicListModel dynamically? Something along the lines of :

    Yes, this page in docs describes it: link. Or you can use a Loader.



  • @sierdzio
    I've already looked at the page Dynamic QML Object Creation from JavaScript ,but I thought the function Qt.createComponent() is only for .qml files , and the createObject object method only works on components created by the Qt.createComponent() method.

    I've tried to create a component manually like so:

    Component {
            id: myTestComponent
            ActorModel {
                id: actModel
                Component.onCompleted: {
                    console.log("ACTORMODEL:ON COMPLETE TRIGGERED")
                    init(dbManager)                
                }
            }
        }
    

    and then use the createObject method on it like this myTestComponent.createObject , but it didn't complile. (Clearly, I'm doing it wrong, but I couldn't find how to use this method on a C++ registered Object in the documentation.)

    I did manage to use a Loader like this:

    Component {
            id: myTestComponent
            ActorModel {
                id: actModel
                Component.onCompleted: {
                    console.log("ACTORMODEL:ON COMPLETE TRIGGERED")
                    init(dbManager)                
                }
            }
        }
            
        Loader{
            id: myTestLoader
            sourceComponent: myTestComponent       
        }
    
    
    ListView{
      id:myList
      model:myTestLoader.item
    }
    

    But I'm unsure as to how to trigger a creation of a new instance.



  • @sierdzio I managed to get it to work with createObject like so:

    Component {
            id: myTestComponent
            ActorModel {
                id: actModel
                Component.onCompleted: {
                    console.log("ACTORMODEL:ON COMPLETE TRIGGERED")
                    init(dbManager)                
                }
            }
        }
    
    Button {
      onClicked: {
        // creates a new instance of ActorModel
         var x = myTestComponent.createObject();     
         myList.model = x;
      }
    }
    
    ListView{
      id:myList
    }
    

    Thank you very much for your help!


  • Moderators

    You're welcome, happy coding :-)

    And thanks for sharing the solution, maybe it will help somebody else, too.


Log in to reply
 

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