How to use things derived from QAbstractListModel as field/property? #914
-
Hi, I know we can create a type derived from QAbstractListModel, such as
Could someone tell me how can I use CustomBaseClass in the filed, so I can access it in qml file?
Thanks |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
Hey, So QML will be wanting a raw pointer in C++, eg This says that there is a Q_PROPERTY which is a pointer under the field inner. cxx-qt/examples/qml_features/rust/src/nested_qobjects.rs Lines 32 to 35 in f82ab4e Then we store a raw pointer to the QObject as a field in the struct cxx-qt/examples/qml_features/rust/src/nested_qobjects.rs Lines 67 to 78 in f82ab4e This will need to go into your FileServerRS. Note this example also shows how to use the pointer with methods and shows how to get from a raw pointer back to a pinned mutable reference (so that you can reach methods on the pointer object. Note that you might need a C++ method to create the raw pointer of the CustomBaseClass if you are doing this in Rust rather than QML. (we have an issue somewhere about generating helpers for this automatically somewhere i think). Feel free to ask more questions as this area is a little tricky :-) |
Beta Was this translation helpful? Give feedback.
-
https://doc.qt.io/qt-6/qabstractlistmodel.html
Is there an example to how use a type derived from QAbstractListModel for a QML ListView where you can add/remove elements and save/load the list? I cannot call the function from the type instance in QML: |
Beta Was this translation helpful? Give feedback.
Hey,
So QML will be wanting a raw pointer in C++, eg
CustomBaseClass*
. We can use Rust raw pointers for this, if we look at thenested_qobjects
example this is similar.This says that there is a Q_PROPERTY which is a pointer under the field inner.
cxx-qt/examples/qml_features/rust/src/nested_qobjects.rs
Lines 32 to 35 in f82ab4e
Then we store a raw pointer to the QObject as a field in the struct
cxx-qt/examples/qml_features/rust/src/nested_qobjects.rs
Lines 67 to 78 in f82ab4e