
Originally Posted by
dude
It's generally easy to implement, except a one specific reqiurement: the customer wants the directory tree to be showing the My Documents folder at the top of the hierarchy, alongside the system logical drives . It's suuposed to let user an easy access to his personal files. I know it sounds kinda silly, but it is what I currently need.
So I was right - the extra item points to some existing folder, correct?
Also using old-style
QTreeWidget would take me to the result in 1-2 days.
True, but then you'd have to implement some functionality to introduce additional things to your application and you'd be spending days biting your nails thinking how to do this while QDirModel would support it out of the box. Convinience classes are nice for simple things, but the more complex the subject, the more problems with them and the more benefit of using a model approach.
If you provide me with an small piece of code, I would be very grateful for your help.
Let's see... I don't have time to write a complete and tested solution, but here are some hints.
Two essential functions are mapToSource() and mapFromSource() in your proxy model. A basic functionality of mapToSource() can look like this:
if(ind.parent().isValid()){
return ind; // this could just work... if not, you need to map it to the source model
}
// parent not valid - drives level
if(ind.row()==0) return mydoc; // return fixed index for the documents folder
return sourceModel()->index(ind.row()-1, ind.column());
}
QModelIndex Proxy::mapToSource(const QModelIndex &ind) const {
QModelIndex mydoc = ((QDirModel*)sourceModel())->index("/home/wysota/Dokumenty");
if(ind.parent().isValid()){
return ind; // this could just work... if not, you need to map it to the source model
}
// parent not valid - drives level
if(ind.row()==0) return mydoc; // return fixed index for the documents folder
return sourceModel()->index(ind.row()-1, ind.column());
}
To copy to clipboard, switch view to plain text mode
If you provide those two methods and the rowCount() like this:
int origrows = sourceModel()->rowCount(mapToSource(index));
return index.isValid() ? origrows : origrows+1;
}
int Proxy::rowCount(const QModelIndex &index) const {
int origrows = sourceModel()->rowCount(mapToSource(index));
return index.isValid() ? origrows : origrows+1;
}
To copy to clipboard, switch view to plain text mode
you should have the basic functionality already working.
Bookmarks