Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    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

    QML and Qt Quick
    2
    3
    643
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • H
      hfxc last edited by

      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

      1 Reply Last reply Reply Quote 0
      • Gianluca
        Gianluca last edited by

        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)
        }
        @

        1 Reply Last reply Reply Quote 0
        • H
          hfxc last edited by

          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.

          1 Reply Last reply Reply Quote 0
          • First post
            Last post