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

How to update combobox values dynamically?

asked 2014-05-27 21:48:37 +0300

ISU gravatar image

I would like to have two comboboxes and the values of the second one should be dependant of the option chosen for the first combobox. However, I have not been able to figure out how to make this happen.

As I understand previously it has been able to refresh the combobox values from C++ like http://stackoverflow.com/questions/8293447/how-to-refresh-contents-of-qcombobox-in-qtableview here. My understanding of the context property is pretty thin but should this be implementable also for Sailfish Silica? http://stackoverflow.com/questions/18616497/how-to-use-models-with-qml

If I understood correctly I would need a signal which signals the change in the selection of the first combobox and somehow update or refresh the second combobox in QML.

Here is my code for comboboxes if it is of any help. I would also appreciate a tutorial on this bridge over C++ and QML. Somehow I found the Qt documentation on this matter rather superficial.

        ComboBox {
            id: comboBox1
            width: parent.width
            x: Theme.paddingMedium
            label: "Combobox1: "
            currentIndex: { comboBox_Olio.getCombo1Idx() }

            menu: ContextMenu {
                MenuItem { text: "Option A"}
                MenuItem { text: "Option B"}
            }

            onCurrentIndexChanged: {
                comboBox_Olio.setComboBox1Value(currentIndex)
                console.log(currentIndex)
            }
        }

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

            menu: ContextMenu {
                Repeater {
                    model: comboBox_Olio.getComboBoxValues()
                    MenuItem { text: modelData}
                }
            }
            onCurrentIndexChanged: {
                comboBox_Olio.setComboBox2( value )
            }
        }
edit retag flag offensive close delete

2 Answers

Sort by » oldest newest most voted
1

answered 2019-05-04 16:17:50 +0300

hsjpekka gravatar image

updated 2019-05-04 16:24:09 +0300

This doesn't probably help you anymore, but perhaps someone else needs advice. And I don't know how to do it in C++, but QML and javascript are enough.

Use ListModel and Repeater to create dynamic content in ComboBoxes:

        ListModel {
            id: data1
            ListElement {
                myData: "dd"
            }
            ListElement {
                myData: "dd"
            }
        }

        ComboBox {
            id: cb1
            label: "combo1"
            menu: ContextMenu {
                Repeater {
                    model: data1
                    delegate: MenuItem {
                        text: myData
                    }
                }
            }
            onCurrentIndexChanged: {
                if (data2.count > 4)
                    data2.clear()
                data2.append({"myData": "row " + currentIndex})
            }

        }

        ListModel {
            id: data2
            ListElement {
                myData: ""
            }
        }

        ComboBox {
            id: cb2
            label: "combo2"
            menu: ContextMenu {
                Repeater {
                    model: data2
                    delegate: MenuItem {
                        text: myData
                    }
                }
            }
            onCurrentIndexChanged: {
                if (data1.count > 4)
                    data1.clear()
                data1.append({"myData": "line " + currentIndex})
            }
        }
edit flag offensive delete publish link more
0

answered 2014-05-27 22:06:46 +0300

Acce gravatar image

updated 2014-05-27 22:10:39 +0300

I'm not 100% sure, but I think the problem is how you set your model in the comboBox2. There is no way of knowing when to reload the model, since you do not use a qml property for it. I suggest you inherit from QObject and give your comboBox_Olio a

Q_PROPERTY(QStringList* myModel READ myModel NOTIFY myModelChanged)

(change QStringList to your model type) and then use the implementation for getComboBoxValues() for myModel(). Then in your QML use

Repeater {
      model: comboBox_Olio.myModel
      ....
 }

and remember to

emit myModelChanged();

whenever the value of the first combobox changes, you could autowire it somehow or simply call reloadSubComboBoxModel() from the QML side.

EDIT: Here's an example which uses a QList of QObject pointers as a model property https://github.com/Acce0ss/simpleweather/blob/master/src/weatherinfo.h , https://github.com/Acce0ss/simpleweather/blob/master/src/harbour-simpleweather.cpp and https://github.com/Acce0ss/simpleweather/blob/master/src/weatherinfo.cpp

edit flag offensive delete publish link more

Comments

Thank you for your prompt reply! Sorry that I have not been able to dedicate more effort on this issue lately. I think the problem currently is exactly this "autowire" or "reloadSubComboBoxModel()" stuff. I can emit signal both from QML and C++ side but I don't know how to tell the combobox to update its contents.

ISU ( 2014-06-17 19:26:19 +0300 )edit
Login/Signup to Answer

Question tools

Follow
2 followers

Stats

Asked: 2014-05-27 21:48:37 +0300

Seen: 13,170 times

Last updated: May 04 '19