help with dependent combo boxes in model using delegate
-
I have QStandardItemModel that is comprised of several columns with the first and second column containing combo boxes. I have implemented a QitemDelegate class in which utilize the createEditor, setEditorData and setModelData and have set openPersistentEditor for the indices containing the combo box.
What I would like to achieve is to have default data in the combo box initially and when the user selects an item in a combo box it affects the data that is contained in the other combo box. For instance combo boxes in column one contain a list of cities and combo boxes in column two contain hotels to those cities. What I would like is to initially have a list of all cities in combo box in col one and list of all hotels in col 2. When a user selects the city Sydney, only hotels belonging in Sydney should be available to choose from in combo box in column two. Also have the same apply vice versa. If initially a hotel is chosen, having only a list of possible hotels in that city to choose from.
I am having difficulty trying to clear the combo box when a selection is chosen. Rather then clearing it, it appends the available list to the existing default list.
Also i am listening to the combo box current Index changed, in which I invoke commitData(qobject_cast<QComboBox*>(sender()) to call on the setModelData function, however the setModelData function gets called in which it populates the data twice in the combo box.
Any suggestions on how to go about setting this up? Should I be using a QStringListModel? How do I prevent the signal from being called twice to the setDataModel?
Sorry for the lengthy post, newbie here!
-
It looks like you are doing a lot of duplication here...
There are a few things to change:- Do not use QitemDelegate, use QStyledItemDelegate as base class. It's the same but the second supports stylesheets so as a general rule always use the second one.
- Do not set openPersistentEditor, if you want it to look like a combobox all the time just reimplement
paint()
on the delegate - Do not populate the combobox from scratch every time, use proxy models as QComboBox is a view in itself
I'll try and implement it and edit this post with the result
EDIT:
Example implemented here: https://github.com/VSRonin/QtComboProxyExample -
This post is deleted!