Change QML Listview delegate from C++

dangsonbk

I am trying to change the delegate of qml listview from C++ but currently I stuck at how to change the alias which represents the delegate property.

Update on details: I have multiple delegates in separated qml files, in my application there are many screens, each screen will have different UI of listview, what I want is something like:

Pass delegate file name to C++ function >>> C++ function set delegate property of listView (or thing like that) >>> listview loads corresponding delegate.

My qml file looks like:

Item {
    id: root
    property alias listViewDelegate: listView.delegate

    ListView{
        id: listView
        delegate: MyDelegate{} // I have MyDelegate.qml file, it's working well
        model: listModel
    }

    // List model
    MyListModel {
        id: listModel
    }
}

I tried to change listViewDelegate alias from C++ using setProperty() method but got no luck (error in fact).

qmlObj->setProperty("listViewDelegate", componentDelegate);

How to achieve this? Or anyone can suggest me the better method to achieve it? Thanks!

dangsonbk

Thank everyone, I just figured out a way to do that by using javascript, seems complicated but it works.

I add this javascript function into my root Item

function loadListViewDelegate(file){
    var component = Qt.createComponent(file);
    if(component && (component.status === Component.Ready))
    {
        listView.delegate = component;
        return file;
    }
    return "";
}

Then I invoke this function from C++, with parameter is the delegate qml file. It looks like this:

QMetaObject::invokeMethod(qmlObj, "loadListViewDelegate", Q_RETURN_ARG(QVariant, returnedValue), Q_ARG(QVariant, "delegate_screen_home.qml"));

How to invoke qml javascript method

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to access QML ListView delegate items from C++?

From Dev

How to access QML ListView delegate items from C++?

From Dev

Changing a model of a QML ListView doesn't change the corresponding delegate

From Dev

How to bind to a signal from a delegate component within a ListView in QML

From Dev

How can I change QT listview background from delegate?

From Dev

Get index of the delegate currently displayed - QML ListView

From Dev

Access Listview currentIndex from Delegate

From Dev

C++/QML: ListView is not updated on dataChanged signal from QAbstractListModel

From Dev

QML TableView access model properties from delegate

From Dev

Delegate reading from QML nested list not updating

From Dev

QML ListView sections from the code

From Dev

QML ListView sections from the code

From Dev

Refresh QML Listview from Slot

From Dev

QML,How to dynamicaly change Item of Repeater from C++

From Dev

BB10 - Change QML Header Title from C++

From Dev

QML,How to dynamicaly change Item of Repeater from C++

From Dev

QML not registering property change from C++ property

From Dev

QML ListView - change all but current item

From Dev

c# ListView cross thread, delegate not working

From Dev

QML Listview header from QAbstractListmodel headerData()

From Dev

QML Listview header from QAbstractListmodel headerData()

From Dev

Filling QML ListView with data from std::map

From Dev

how can i change the text of the button without clicked in the listview delegate?

From Dev

A dynamic C++ model and a QML ListView

From Dev

Reusing a delegate component in QML

From Dev

Reusing a delegate component in QML

From Dev

Use QML delegate on QListWidget

From Dev

QML - connect a signal from c++ to qml

From Dev

C# Reflection Delegate exception: Must derive from Delegate

Related Related

  1. 1

    How to access QML ListView delegate items from C++?

  2. 2

    How to access QML ListView delegate items from C++?

  3. 3

    Changing a model of a QML ListView doesn't change the corresponding delegate

  4. 4

    How to bind to a signal from a delegate component within a ListView in QML

  5. 5

    How can I change QT listview background from delegate?

  6. 6

    Get index of the delegate currently displayed - QML ListView

  7. 7

    Access Listview currentIndex from Delegate

  8. 8

    C++/QML: ListView is not updated on dataChanged signal from QAbstractListModel

  9. 9

    QML TableView access model properties from delegate

  10. 10

    Delegate reading from QML nested list not updating

  11. 11

    QML ListView sections from the code

  12. 12

    QML ListView sections from the code

  13. 13

    Refresh QML Listview from Slot

  14. 14

    QML,How to dynamicaly change Item of Repeater from C++

  15. 15

    BB10 - Change QML Header Title from C++

  16. 16

    QML,How to dynamicaly change Item of Repeater from C++

  17. 17

    QML not registering property change from C++ property

  18. 18

    QML ListView - change all but current item

  19. 19

    c# ListView cross thread, delegate not working

  20. 20

    QML Listview header from QAbstractListmodel headerData()

  21. 21

    QML Listview header from QAbstractListmodel headerData()

  22. 22

    Filling QML ListView with data from std::map

  23. 23

    how can i change the text of the button without clicked in the listview delegate?

  24. 24

    A dynamic C++ model and a QML ListView

  25. 25

    Reusing a delegate component in QML

  26. 26

    Reusing a delegate component in QML

  27. 27

    Use QML delegate on QListWidget

  28. 28

    QML - connect a signal from c++ to qml

  29. 29

    C# Reflection Delegate exception: Must derive from Delegate

HotTag

Archive