PDA

View Full Version : Multiple table delegates or a single table delegate?



karankumar1609
22nd August 2014, 10:00
Hello All,

I am currently working on a project in which i have to use QTableView multiple times in different windows. But delegates will be different for each tables.
As i said delegates will be different for QTableView, i want to know the right way to do that.
Should i integrate all delegates in a single .h and .cpp, or should i use different delegate class for each.

I know the answer will be very easy for you, but i want to know your views on that.
It will be very helpful to me for creating a better project and for my knowledge too. :) :o

Thanks

d_stranz
22nd August 2014, 17:33
Delegates are attached to a table view, not to the model.

The QAbstractItemDelegate::createEditor() method supplies the QModelIndex as an argument. It will contain row and column (0,0) in both cases, but no information about which table is asking for the editor. So if you put all of the code for managing the editors into one class, you will have no way to determine which editor to create. Even if you use different editors for different rows or columns in the various view, your code still has no way to determine which table view is being edited, so you'll end up creating the wrong editors.

If you are asking, should you put the code for all of the delegate classes into the same source files, the answer is no. That's just a bad coding practice. If you make a change to one delegate class, then every table view will class will have to recompile even if it doesn't use the delegate that was changed.

ChrisW67
22nd August 2014, 22:28
If most of the views display most of the data in exactly the same way with a few exceptions then I would suggest a set of delegate classes (usually by data type) that you attach to the relevant columns (rows) of each view, e.g. ConditionallyColouredNumberDelegate on column zero in some views and AlwaysHotPinkDelegate in others.