Provide the model to a ComboBox which is a delegate inside a TableView which has already a model.
-
Hi, Qt'ers
I have a
TableView
whose column uses aComboBox
as a delegate. I have also a model for this view (which I inherit from theQAbstractTableModel
and override all necessary member-functions:data
,rowCount
,setData
, etc.). Let's call this model astableModel
. Everything works fine for now.The question is about those comboboxes: the
ComboBox
element also have themodel
property which I'ld like to use.So this is a sketch of the code.
//Main.qml / /imports Item { property var tableModel //set from C++ (for the sake of simplicity) TableView { id: tableView model: tableModel delegate: DelegateChooser { DelegateChoice { column: 0 // CheckBox for the first column delegate: CheckBox { checked: model.display onToggled: model.edit = checked } } DelegateChoice { column: 1 // ComboBox for the second column <------ delegate: ComboBox { currentIndex: display // This value is from the tableModel model: // WHAT SHOULD IT BE ? } | } } }
This how it looks like (the combo of interest is in the Data Type column):
So the
tableModel
stores the current index only -- not all possible values available to select from.Please, note: The example I've shown is a demo -- the real table consists of more columns and has more different delegates.
Of course, I could've done this:
... model: ["Numeric", "String"] onAccepted: edit = currentIndex
but I would like to avoid the hardcoded values in QML. So what would you do in my situation, provided the model is available from C++ ?
Thank you in advance !
-
@LRDPRDX your combo box needs its own model to define the drop down values. Are these values already part of your table model? Or are they at least easily accessible in the implementation of your table model? If so, one possibility is to define a new role in your model that provides the allowed values for the combo. This could be a simple string list value as
ComboBox
accepts a string list as a model.DelegateChoice { column: 1 // ComboBox for the second column <------ delegate: ComboBox { currentIndex: display // This value is from the tableModel model: optionValues // new role exposed by table model } }
-
@Bob64 Hmm, It sounds cool actually. Natural and simple. I will try this.
@Bob64 said in Provide the model to a ComboBox which is a delegate inside a TableView which has already a model.:
Or are they at least easily accessible in the implementation of your table model?
Yes, they are accessible from the
tableModel
. I mean they are on the C++ side so I can easily integrate the access to them into thetableModel
class.Thank you very much.
-