0

Currently I'm working with the QT-framework via PyQT. I have created a custom object (inheriting from QObject) and can use it as expected from QML. I have also created a delegate in QML, which takes care of the visual representation of this object and is also working as expected.

However, now I want to show a list of several of these objects (with the presentation as defined in the delegate) in a ListView (in QML again). I cannot seem to figure out how this is possible, so can someone give me some pointers?

Thanks in advance for your time.

Edit: In response to some reactions I'll try to clarify what I want to achieve a little bit more with an example. Here we have a rectangle (this is just for clarity purposes, normally this would be my custom object) with a single custom attribute (customText) and a delegate which simply defines the layout, both of which are working.

ListView { height: 1000 model: Rectangle { property string customText: "1.jpg" } delegate: Rectangle { width: 200 height: 200 Text { anchors.fill: parent text: model.customText } } } 

However, now I basically don't want a single rectangle (which wouldn't make a lot of sense in a ListView), but several of them in for example a list. What I would expect is that I would have to change model to something like this, but then I get "Cannot assign multiple values to a singular property ":

 model: [Rectangle { property string customText: "1.jpg" }, Rectangle { property string customText: "1.jpg" }, Rectangle { property string customText: "1.jpg" }] 

Please let me know if you need any further information.

5
  • 1
    Why don't you post here all the code related to the problem instead of explaining it? Commented Jul 31, 2017 at 6:03
  • One way is to inherit from QAbstractListModel and define a model that gives away properties of each of these objects. Commented Jul 31, 2017 at 8:44
  • @folibis Because I tried quite a lot and I have no idea which code is closest to achieving what I actually want, so I think I'm actually looking for how QT is meant to solve this issue. However, I have now included a code snippet which should clarify the issue somewhat. Commented Jul 31, 2017 at 9:52
  • You can't create objects like this within the declaration of a JS array.. how QT is meant to solve this issue is to do as Velkan proposed - creating a descendent of QAbstractListModel that holds your data. The properties of the QObject that you have created should be transformed into roles. Commented Jul 31, 2017 at 10:59
  • Ok, according to the documentation model could be either ListModel, XmlListModel or VisualItemModel. In your case VisualItemModel is more suitable for your needs .See this example for more info. Commented Jul 31, 2017 at 13:08

1 Answer 1

1

You can use ListModel and ListElement qml types. Sample implementation for customized model :

ListModel { id: modelElement ListElement { customText : "1.jpg"} ListElement { customText : "2.jpg"} ListElement { customText : "3.jpg"} ListElement { customText : "4.jpg"} } ListView { height: 1000 model: modelElement delegate: Rectangle { // You can use your custom object here width: 200 height: 200 Text { anchors.fill: parent text: customText } } } 

ListElement represent items in a list that will be displayed using ListView or Repeater items Ref.

You can define you custom object as delegate and then assign the customText to Text element.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.