How to use the source file that I passed the path in a property of the Item as model/delegate of the ListView inside the Item



  • Hello,

    Im trying to create an Item where I can pass as a property an string containing the path to a source file that contains the model/delegate/both and use them in the ListView inside the item

    @MyItem {
    modelSource: "MyModel1.qml"
    delegateSource: "MyDelegate1.qml"
    }
    MyItem {
    modelSource: "MyModel2.qml"
    delegateSource: "MyDelegate2.qml"
    }@

    Some stuff that i tried was

    @Loader {
    id: model
    source: modelSource
    }

    Loader {
    id: delegate
    source: delegateSource
    }

    ListView {
    model: model
    delegate: delegate
    }@

    @ListView {
    model: Qt.createComponent(modelSource)
    delegate: Qt.createComponent(delegateSource)
    }@

    And some variations of that, but none seemed to work, some return reference errors from delegate not seeing the model and others just doesnt draw anything on the screen. I also tried using VisualDataModel and loading them but same results

    Is there anyway to do what I want or I should create a few versions of the Item each one with their model/delegate?

    Thanks in advance



  • I'm not sure how to resolve, but looking at your solution I notice one thing:

    The model require an object (an instantiated component like ListModel, etc), while delegate require a component (not an object).

    Your first solution create two objects, so maybe delegate do not get the right thing.
    Your second solution create two components, so maybe this time is the model that do not get the right thing.

    Try to mix them.

    @
    Loader {
    id: model
    source: modelSource
    }

    ListView {
    model: model.item
    delegate: Qt.createComponent(delegateSource)
    }
    @



  • It worked! Thanks.

    But my problem now is a seg fault with a few Elements in the ListModel. With only 8 elements, I get instant seg fault when I try to run the application. Im now trying to solve it.

    Thanks again.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.