Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add statically object to ListModel

Tags:

qml

qtquick2

I need to create a ListModel, that contains an object (string and bool inside) statically. If I add to an empty ListModel element by using append - all works well.

property ListModel qwe: ListModel {}
var imageToAdd { value: "picture.png", imageType: 1 }

qwe.append({
    text: "TextToAdd",
    image: imageToADD,
    position: 1
})
// This works correct

But I need to create ListModel statically and it doesn't work.

ListModel {
    ListElement {
        text: "TextToAdd"
        image: { value: "Qwer.png", imageType: 1 }  // <-- This doesn't work
        position: 1
    }
}

How it should look like?

like image 351
Anatoliy Leshin Avatar asked Dec 12 '13 07:12

Anatoliy Leshin


2 Answers

A ListElement in Qt must have values of type string, bool, numbers or enum. More complex datatypes like hashmaps are not allowed.

You can read this deep down in the Qt 5.2 sourcecode: qqmllistmodel.cpp. This didn't change since the Qt 4.7 times.

List elements are defined inside ListModel definitions, and represent items in a list that will be displayed using ListView or Repeater items.

List elements are defined like other QML elements except that they contain a collection of role definitions instead of properties. Using the same syntax as property definitions, roles both define how the data is accessed and include the data itself.

The names used for roles must begin with a lower-case letter and should be common to all elements in a given model. Values must be simple constants; either strings (quoted and optionally within a call to QT_TR_NOOP), boolean values (true, false), numbers, or enumeration values (such as AlignText.AlignHCenter).

However, a ListModel seems to be capable to store all types defined in the ECMA-262 standard: The primitive types, which are Undefined, Null, Boolean, Number, and String as well as the Object type.

Edit: If you want to create the elements in QML, you have to rewrite your code to something like

ListModel {
    ListElement {
        text: "TextToAdd"
        imageValue: "Qwer.png"
        imageType: 1
        position: 1
    }
}

Edit2: Or you go the Javascript way. Create an empty model first and fill it on start

ListView {
    model: ListModel { id: qwe }
    delegate: ... 

    Component.onCompleted: {
        qwe.append({
            text: "Image 1",
            image: { value: "picture.png", imageType: 1 },
            position: 1
        });
        qwe.append({
            text: "Image 2",
            image: { value: "picture.png", imageType: 1 },
            position: 2
        });
        qwe.append({
            text: "Image 1",
            image: { value: "picture.png", imageType: 1 },
            position: 3
        });
    }
}
like image 142
Simon Warta Avatar answered Oct 20 '22 14:10

Simon Warta


model: ListModel {
    ListElement {
        name: "My Name"
        image: ListElement {
            src: "My src"
        }
    }
}

You can access it in for example a delegate:

image.get(0).src

I would asume you should be able to access it via image.src but that doesn't work...

like image 2
RvdK Avatar answered Oct 20 '22 14:10

RvdK