declare property of pointer in qml
-
ok, I think it's not clear what I want.
- I have a list with many objects in it (qList<ClassNameOfModel*>).
- I display the list with a combobox (list is assigned as model to combobox)
- I select one item in that combobox
until here, no problem.
Now I want to pass the selected item (ClassNameOfModel) to my common edit-component (qml-file) to display and edit some item-properties.
This common edit-component looks like this:editComponent.qml: Item { property ClassNameOfModel *dataModel << This doesn't work TextField { text: dataModel.text1 ... } TextField { text: dataModel.text2 ... } TextField { text: dataModel.text3 ... } ... }
How can I pass data to my custom-component?
-
???
editComponent { id: edit1 }
My idea was to use onCurrentIndexChanged to assign the selected model to my editComponent:
configComboBox.onCurrentIndexChanged: { edit.dataModel: configComboBox.model[configComboBox.currentIndex] }
-
@themts:
It doesn't work.
I register my dataModelClass like this:
qmlRegisterUncreatableType<ClassNameOfModel>("com.test.application", 1, 0, "ClassNameOfModel", "no no no");
As soon as I start my application I see the output:... qrc:/ConfigEdit.qml:3 Type ConfigEditEditForm unavailable qrc:/ConfigEditEditForm.ui.qml:6 no no no
-
@themts:
It doesn't work.
I register my dataModelClass like this:
qmlRegisterUncreatableType<ClassNameOfModel>("com.test.application", 1, 0, "ClassNameOfModel", "no no no");
As soon as I start my application I see the output:... qrc:/ConfigEdit.qml:3 Type ConfigEditEditForm unavailable qrc:/ConfigEditEditForm.ui.qml:6 no no no
-
@themts:
It doesn't work.
I register my dataModelClass like this:
qmlRegisterUncreatableType<ClassNameOfModel>("com.test.application", 1, 0, "ClassNameOfModel", "no no no");
As soon as I start my application I see the output:... qrc:/ConfigEdit.qml:3 Type ConfigEditEditForm unavailable qrc:/ConfigEditEditForm.ui.qml:6 no no no
@themts This is a known problem with registering an uncreateable type. Its because QML is declarative. See this ticket https://bugreports.qt.io/browse/QTBUG-36752?jql=text ~ "Uncreatable C%2B%2B type can't be used for QML property types" . You have to just do QmlRegisterType<>. If you read the docs for qmlRegisterUncreatableType it says "This is useful where the type is only intended for providing attached properties or enum values.".
-
@Buttink:
I already tried qmlRegisterType<ClassNameOfModel>() but then I get the message:
ClassNameOfModel is not a type
A workaround for me was now to declare a generic property var.
This way I have no code-completition but it works. Of course I would prefer a typesafe way.I think it only works if I register the class as a creatable object, but then I have to implement a copyconstructor etc.
-
@Buttink:
I already tried qmlRegisterType<ClassNameOfModel>() but then I get the message:
ClassNameOfModel is not a type
A workaround for me was now to declare a generic property var.
This way I have no code-completition but it works. Of course I would prefer a typesafe way.I think it only works if I register the class as a creatable object, but then I have to implement a copyconstructor etc.
@themts Hmmm that sounds really odd, but without the code, I don't know why it would output that error. I will say that you do not need a copy constructor. You could make a new qml type from
class Thing : public QObject { Q_OBJECT public: explicit Thing(QObject* parent = nullptr); virtual ~Thing(); };