PDA

View Full Version : How to use a different model in a delegate in a model-view construction



Meladi
5th February 2014, 13:33
Hello all, I hope you can help me with this.

The situation is as follows: I have two widgets, a listView and a tableView. Both have their own models (listmodel and tablemodel - the data is not really related).
The listView is based on a QStringList filled with options, the user can add and remove as well.
The tableview shows the content of a file. The headers of its columns should all be comboboxes, in which you select which option from the listView is applicable to the column beneath it.

Is there an elegant/proper way to set the listmodel as the model for my ComboboxDelegates? Or is there perhaps a way to have a third model for the ComboBoxDelegates which synchronizes with the listmodel in real time?

Any help is appreciated.

anda_skoa
5th February 2014, 13:54
QComboBox has a method called setModel()
Call that with your list model.

Cheers,
_

Meladi
5th February 2014, 14:08
Thanks for your reply.

The problem is that the QComboBox is a delegate of my tableView, and it doesn't know the listmodel at all! The tableView has its own model, the tablemodel. The listmodel is created when the ui is constructed.

Here's a picture to clarify what I mean:

10016

My UI class creates the models and attaches them to the views. The TableModel class is responsible for the data inside the table, including the ComboBoxDelegates. And those have no connection with the listView's listmodel at this point. If I want to set the model for those comboboxes, the code doesn't compile (error: a nonstatic member reference must be relative to a specific object) - the model is dynamic.

anda_skoa
5th February 2014, 15:39
The listmodel is created when the ui is constructed.

At which point you probably have access to the list model, no?
I.e. pass it to the delegate's constructor or have a method like setComboBoxModel(...)

Cheers,
_