Question

I have a Qt C++ application that uses several different models (ultimately) based on QAbstractItemModel.

Several of the items in these models use a custom Delegate to render them properly, these work well.

However, I'm not sure how best to specify which of these Delegate classes should be used to render the data.

At present, I'm calling QAbstractItemView::setItemDelegateForColumn() in each of the forms that contain a view.

This feels very clumsy, because it relies on the form classes knowing which delegate should be used for each column in every model - and if that changes in the future, I have to update every form.

Furthermore, in some cases a View is switching between two different models that require different Delegates, so for these the form class has to handle that switch as well.

Is there a better way to do this?

Was it helpful?

Solution

The model must not know about representation, so you should deal with the view. You can subclass involved view class (e.g. QTableView) and reimplement setModel virtual method. In your implementation call the parent class implementation and adjust item delegates based on specified model. Put objects of this class inside all your forms. So forms will not know about delegates. I think this approach agrees with the Model-View conception.

OTHER TIPS

AFAIK, there's no better way. However, instead of updating manually every form, i used to have an enum which holds the columns index, which is used both in the model and in the view to retrieve data and apply delegates.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top