[Qt-interest] Adding multiple checkboxes to a QTreeView with QFileSystemModel

Andre Somers andre at familiesomers.nl
Mon Jul 12 21:37:02 CEST 2010


Op 12-7-2010 14:57, Sven Grunewaldt schreef:
> Hi,
>
> I need multiple (currently 3, maybe more in the future) Checkboxes in
> front of the first column in a QTreeView. The view is populated by a
> QFileSystemModel.
>
> All the examples I can find are about adding ONE checkbox which can be
> achieved by setting the checkable-flag or so. Sadly this doesn't work
> for more than one checkbox...
>
> I tried QItemDelegate but I don't understand how I should use a delegate
> to add multiple checkboxes in ONE column, I guess this class is not made
> for stuff like this.
> After that I tried subclassing QFileSystemModel and implementing my own
> data() function which calls the parent function when the column is
> higher than 0, but this leads to access violations and other funny stuff...
>
> How can I achieve this? I'm at a total loss right now...
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest
>
>    
You have two issues to address: the first is to actually show the check 
boxes, but you will also have to think about how you are going to store 
the check state.

The ultimate solution depends on your exact needs. Would it be 
acceptable for you to have the checkboxes in separate columns? Or do you 
need them all in the first column?
If separate columns are acceptable, then I think I would prefer using a 
method where you use a proxy model to add columns to an existing model, 
and keep track of the checkstate of each of the items. That would be the 
simplest option by far.

However, if you do need to have all three checkboxes in the same column, 
you have no option than to use a delegate. Drawing the checkboxes can be 
done using the methods supplied by QStyle. Look at how 
QStyledItemDelegate is implemented for inspiration. It is not trivial! 
You also need to take care to handle mouse clicks on the items 
(QAbstractItemDelegate provides a method for this), as well as think of 
a way to store the state of the items.

André




More information about the Qt-interest-old mailing list