Solved Construct a QML object with parameters
-
I derived a sub-class of QSqlTableModel, let's say SqlTableModel.
In the C++ code, I can create an instance of the SqlTableModel by SqlTableModel *myModel = SqlTableModel(0, db), where the db is a QSqlDatabase instance.
Then I register this class to QML by qmlRegisterType<SqlTableModel>("QmlSql", 1,0, "SqlTableModel);
My question is, in the QML code, how can I passed the db parameter to the SqlTableModel object?
-
Hi! See Exposing Attributes of C++ Types to QML, it shows how to expose properties and methods.
-
@Wieland I have read through this reference, but it does not allow to expose a QSqlDatabase property to QML by declaring:
Q_PROPERTY(QSqlDatabase db READ db WRITE setDb).I think this is because there is no such type in the QML type system, so the QML does recognize it.
-
Custom types for QML have to inherit from QObject.
-
@Wieland Ah ha, yeah, I just forgot this point. Thank you so much for your help!