PDA

View Full Version : QDirModel - add own items



heinz32
10th May 2008, 10:04
Hi,

i need a folderbrowser with QDirModel in my Application, but i need to add my own icons (toplevel as well as subentries) (for network access for example). Is there an easy way to do this?

thank you very much in advance

wysota
10th May 2008, 16:46
No, there is no easy way to do that. For a non-trivial solution search the forum - the problem has been raised at least two times before.

heinz32
11th May 2008, 00:12
i did search and did not find anything (search terms: Qdirmodel, insert items, insert icons, qtreeview items) - could you point me to the thread(s) please?

wysota
11th May 2008, 01:32
Try "subclass QDirModel".

heinz32
11th May 2008, 12:46
ah thank you very much , i have found something according to my needs.
if i encounter further problems (which will be most likely the case ;)) i will ask again

heinz32
11th May 2008, 16:48
ok now i subclassed QDirModel::data and QDirModel::rowCount as following:



int MyDirModel::rowCount(const QModelIndex & parent)const
{
if(parent == QModelIndex())
return QDirModel::rowCount(parent) + 1;

return QDirModel::rowCount(parent);
}

QVariant MyDirModel::data(const QModelIndex & index, int role)const
{
if(index.row() == (QDirModel::rowCount(QModelIndex()) + 1))
{
if(role == Qt::DisplayRole)
return QString(tr("bla"));

if(role == Qt::DecorationRole)
return iconProvider()->icon(QFileIconProvider::Desktop);

return QVariant();
}

return QDirModel::data(index, role);
}


it does work, however i cannot add an item at the toplevel, only as subentries of other items... I use a treeview to display this, how can i get my item as a toplevel?

edit: the problem seems to be: rowcount isnt called at toplevel
edit 2: it is called... why doesnt it show my additional item then :(((

wysota
11th May 2008, 18:15
if(index.row() == (QDirModel::rowCount(QModelIndex()) + 1))

should be:


if(index.row() == QDirModel::rowCount(QModelIndex()))

Rows are numbered from 0 to rowCount()-1 inclusive.

heinz32
11th May 2008, 18:30
does not change anything, it now just replaces the ".." (because rowcount returns 1 at toplevel) of every folder with my item but still no extra toplevel item

wysota
11th May 2008, 19:55
Because you should check the parent as well :)

heinz32
11th May 2008, 22:43
yes but thats not the problem, it does not add any extra items it just replaces the ones which are already there, if i do the parentcheck nothing happens at all

edit: i did a check, MyDirModel::data is only called for the one regular row there, not for my additional one. how can i tell Qtreeview to request that one?

heinz32
12th May 2008, 13:21
ok i think the problem is that i have to subclass index() too, because the treeview does request my additional row there. However, i do not know what to return - if i return an index from another item already existing in Qdirmodel, it adds that one, but if i return my own index created with createIndex, the program just crashes ... is there really no solution for this? i would consider this a very basic requirement but it just wont work :(

wysota
15th May 2008, 11:23
You have to reimplement all methods that might want to try accessing internal data through the index unless you can craft the internal representation as well (and return it using createIndex()). For instance, if you wanted to add "My Documents" entry to the model, you could fetch an index of the real path to My Documents, access its internal pointer and use it with your index. That could have a chance to work. Otherwise it might be simpler to add items by implementing a simple proxy model so that the original dir model remains stable.