[Qt-interest] QAbstractProxyModel methods index, parent, rowCount, columnCount not implemented?

Stephen Kelly steveire at gmail.com
Fri Jun 3 21:01:14 CEST 2011


Andre Somers wrote:

<snip>
> This
> leads to code like this:
> 
> QModelIndexListselectedFiles;
> 
>      QModelIndexList  selectedDirectories;
> 
>      QModelIndexList  unselectedFiles;
> 
>      QModelIndexList  unselectedDirectories;
> 
>      m_checkProxy->checkedState()
> 
>              .checkedLeafIndexes(selectedFiles)
> 
>              .checkedBranchIndexes(selectedDirectories)
> 
>              .uncheckedLeafIndexes(unselectedFiles)
> 
>              .uncheckedBranchIndexes(unselectedDirectories);
> 
> 
> This way, there are no QModelIndexLists that are kept around in the
> model any more, and you can still get the different lists in an
> efficient way. The CheckableProxyModelState design should encourage
> using it only in the designed way: as a result of
> CheckableProxyModel::checkedState() to be discarded after the call.

I've read the code now.


I noticed in 
getCombinedChildrenCheckState(QModelIndex sourceIndex) const

you use sourceIndex.child(0,0).

This won't return what you expect if sourceIndex is invalid. The solution is 

if (sourceIndex.isValid())
  sourceIndex.child(0,0);
else 
  model->index(0, 0, sourceIndex);

But of course if you have to do that anyway, the better solution is to just 
use model->index(0, 0, sourceIndex); unconditionally. I think child() should 
be removed in Qt5.


In checkedState() you seem to add indexes in the source model to the data 
members of the CheckableProxyModelState, but then you use mapToSource on the 
same indexes.


I'm still surprised there's no better way using things like QItemSelection 
etc to achieve what you're trying to do, though I've never attempted to 
implement the 'partially checked nodes when 1 or more children are checked' 
or related features.
Nothing else jumped out to me as something that will break in an obvious 
situation, but I don't really like the API of creating and passing in 
QModelIndexLists.

There's also (unlikely I guess) ways to break it like this maybe:

    CheckableProxyModelState &state = m_checkProxy->checkedState();

    // check some check boxes
    // which might break the stuff below.
       state.checkedLeafIndexes(selectedFiles)
            .checkedBranchIndexes(selectedDirectories)
            .uncheckedLeafIndexes(unselectedFiles)
            .uncheckedBranchIndexes(unselectedDirectories);


Finally, it looks like you could use a string template system in 
MainWindow::selectedItemsChanged :)


All the best,

Steve.







More information about the Qt-interest-old mailing list