Map int to strings in WTableView for implementing ComboBox

Added by Thomas Suckow over 1 year ago

I am implementing a ComboBox in a WTableView. I have the delegate setup and should be able to map the index to the row in the ComboBox (The implementation is not yet complete but does display the ComboBox as expected).

What I am having trouble with is mapping the int to text strings when NOT editing. My belief is that I will need to re-implement the update(...) method.

Is there an easier way?


Replies

RE: Map int to strings in WTableView for implementing ComboBox - Added by Koen Deforche over 1 year ago

Hey Thomas,

It is your model's responsibility to provide the correct data for DisplayRole (the text) and EditRole (the int). When modifying the selected value using setData(), the model should reflect this in an updated DisplayRole.

In that case you will not need to reimplement the update() method.

Regards,
koen

RE: Map int to strings in WTableView for implementing ComboBox - Added by Thomas Suckow over 1 year ago

After I implemented it, I thought about that. Its almost less elegant then because both the delegate and the model have to know about the structure of the combobox.

RE: Map int to strings in WTableView for implementing ComboBox - Added by Koen Deforche over 1 year ago

Hey,

I do believe it is the right way: there is a contract between the model and the item delegate w.r.t. how roles are used for getting and setting data. Ideally, you would want the combobox to be populated using a WAbstractItemModel* which is also set as a particular role (e.g. EditOptionsRole). In that way the item delegate does not require any configuration other than being set to the view, and it itself finds all the necessary data for working in the model.

Regards,
koen

RE: Map int to strings in WTableView for implementing ComboBox - Added by Thomas Suckow over 1 year ago

Well... When you put it that way. ;)

I'll see about implementing this.