Home > Blockchain >  Is it possible to create binding proxy in qt qml?
Is it possible to create binding proxy in qt qml?

Time:11-12

i have a cpp struct which have so much fields

struct CloudMusicSongList{ ... };

and i want use it in qml in order input some infomation to it's instance, but i don't want to create a qobject derived class,and create a lot of qproperty ... i seache in google and get this code snippt from this enter image description here

i don't know whether it is the X Y problem.
somebody can help? thanks

i want create a bindingProxy template class which twoway bindable in qml i think it should equivalence dynamicObject in wpf

CodePudding user response:

Based on CloudMusicSongList I'm assuming the thing that you have is a list/array. There are quite a number of ways of exposing this to QML.

QQmlListProperty is a list of QObjects. It requires the underlying QObject be registered, but, allows you to create/manage and walk through a list of the results. The list is owned, usually by another QObject. e.g. we could have a QmlFile object, then implement a QmlFileSystem object that can return a list of QmlFiles.

QAbstactListModel can be used in places where a model is required, e.g. ListView.

QVariant, QVariantList and QVariantMap is the simplest way of creating a Javascript object in QML. The advantage is you do not have to register anything, but, the disadvantage is you do not get intellisense.

QVariant getBooks() {
    QVariantList books;
    QVariantMap book1;
    book1["title"] = QString("Lord of the Rings");
    QVariantMap book2;
    book2["title"] = QString("The Hobbit");
    books.append(book2);
    return books;
    // results in QML/JS:
    // [ { "title": "Lord of the Rings" },
    //   { "title" : "The Hobbit" } ]
}
  • Related