declare property of pointer in qml
-
wrote on 31 Mar 2015, 15:24 last edited by themts
Hey guys,
I have a view with several edit fields. Now I want to instantiate such view and pass a datamodel to it which will be used to fill the edit-fields etc.
How can I declare a property of pointer for my datamodel?If I try this:
Item { id: thisIsMyCustomView width: 200 height: 400 property ClassNameOfModel dataModel //property ClassNameOfModel *dataModel << This doesn't work ... TextField { text: dataModel.text1 ... } TextField { text: dataModel.text2 ... }
it would instanciate an object of ClassNameOfModel.
CU
mts -
Hey guys,
I have a view with several edit fields. Now I want to instantiate such view and pass a datamodel to it which will be used to fill the edit-fields etc.
How can I declare a property of pointer for my datamodel?If I try this:
Item { id: thisIsMyCustomView width: 200 height: 400 property ClassNameOfModel dataModel //property ClassNameOfModel *dataModel << This doesn't work ... TextField { text: dataModel.text1 ... } TextField { text: dataModel.text2 ... }
it would instanciate an object of ClassNameOfModel.
CU
mts@themts What is datamodel ?
-
wrote on 1 Apr 2015, 08:39 last edited by
datamodel is a c-class.
-
-
wrote on 1 Apr 2015, 09:55 last edited by themts 4 Jan 2015, 09:55
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?
-
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?
@themts Ok how do you call or load
editComponent.qml
? -
wrote on 1 Apr 2015, 10:40 last edited by
???
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] }
-
???
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 AFAIK you can do this
property ClassNameOfModel dataModel
Have done something similar but with
Rectangle
.
What error do you get if you do it ? -
wrote on 1 Apr 2015, 10:51 last edited by
This declaration would create an instance of ClassNameOfModel.
-
@themts I guess no. You can check it by printing
dataModel
. It should benull
. -
wrote on 1 Apr 2015, 12:08 last edited by
@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 Can you post the complete compilable example ? Getting confused now O.o
-
@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
wrote on 1 Apr 2015, 13:31 last edited by@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.".
-
wrote on 1 Apr 2015, 13:39 last edited by themts 4 Jan 2015, 13:41
@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.
wrote on 1 Apr 2015, 14:02 last edited by@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(); };
4/15