We have moved to a new Sailfish OS Forum. Please start new discussions there.
1

problem with assigning a model to a combobox [answered]

asked 2018-11-06 08:54:17 +0300

pawel gravatar image

updated 2018-11-06 11:11:59 +0300

Spam Hunter gravatar image

i am trying to populate combobox with an list according to qt doc: http://doc.qt.io/qt-5/qml-qtquick-controls-combobox.html

it seems pretty simple:

ComboBox {
    currentIndex: 2
    **model**: ListModel {
        id: cbItems
        ListElement { text: "Banana"; color: "Yellow" }
        ListElement { text: "Apple"; color: "Green" }
        ListElement { text: "Coconut"; color: "Brown" }
    }
    width: 200
    onCurrentIndexChanged: {
        console.debug(cbItems.get(currentIndex).text + ", " + cbItems.get(currentIndex).color)
    }
}

however when i do try that i get this at run time: Cannot assign to non-existent property "model" and a M16 error in editor

any idea how to solve that ? i am using these imports

import QtQuick 2.2 import Sailfish.Silica 1.0

Thanks !

edit retag flag offensive reopen delete

The question has been closed for the following reason "the question is answered, an answer was accepted" by pawel
close date 2018-11-06 13:11:20.647117

Comments

please learn how to format text when posting code :)

Spam Hunter ( 2018-11-06 11:12:20 +0300 )edit

thanks for sharing..

cleanlabpos77 ( 2018-11-06 11:36:12 +0300 )edit

2 Answers

Sort by » oldest newest most voted
2

answered 2018-11-06 10:32:32 +0300

michfu gravatar image

updated 2018-11-06 11:14:27 +0300

There is no "model" in ComboBox.

You could e.g. try to use Repeater inside of ContextMenu.

edit flag offensive delete publish link more

Comments

i have tried with repeater and yes i get 3 rows need to check how to map attributes of datamodel to menuitem

pawel ( 2018-11-06 12:36:50 +0300 )edit
1

this works:

        ComboBox {
                    id: comboBox2
                    width: parent.width
                    x: Theme.paddingMedium
                    label: "Combobox2: "

                    menu: ContextMenu {
                        Repeater {
                            model: ListModel {
                                id: cbItems
                                ListElement { text: "Banana"; color: "Yellow" }
                                ListElement { text: "Apple"; color: "Green" }
                                ListElement { text: "Coconut"; color: "Brown" }
                            }
                            MenuItem { text: model.text
                            }
                        }
                    }
                }
pawel ( 2018-11-06 13:06:14 +0300 )edit
2

answered 2018-11-06 12:23:43 +0300

pinniini gravatar image

updated 2018-11-06 13:03:07 +0300

This should work, sorry cannot test currently.

ComboBox {
id: combo
width: 200
label: "Combobox label"

menu: ContextMenu {
    MenuItem {
        text: "Banana"
        onClicked: console.debug("Banana" + ", " + "Yellow")
    }
    MenuItem {
        text: "Apple"
        onClicked: console.debug("Apple" + ", " + "Green")
    }
    MenuItem {
        text: "Coconut"
        onClicked: console.debug("Coconut" + ", " + "Brown")
    }
}

(edit) Or use the previously mentioned Repeater with the model:

ListModel {
    id: cbItems
    ListElement { itemText: "Banana"; color: "Yellow" }
    ListElement { itemText: "Apple"; color: "Green" }
    ListElement { itemText: "Coconut"; color: "Brown" }
}

ComboBox {
    id: combo
    width: 200
    label: "Combobox label"

    menu: ContextMenu {
        Repeater {
            model: cbItems
            MenuItem { text: itemText; onClicked: {
                    console.debug(itemText + ", " + color)
                }
            }
        }
    }
}
edit flag offensive delete publish link more

Comments

1

yes, thanks yes !

pawel ( 2018-11-06 13:08:06 +0300 )edit

Question tools

Follow
1 follower

Stats

Asked: 2018-11-06 08:54:17 +0300

Seen: 522 times

Last updated: Nov 06 '18