Results 1 to 12 of 12

Thread: QDirModel - add own items

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1

    Default QDirModel - add own items

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QDirModel - add own items

    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.

  3. #3

    Default Re: QDirModel - add own items

    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?

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QDirModel - add own items

    Try "subclass QDirModel".

  5. #5

    Default Re: QDirModel - add own items

    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

  6. #6

    Default Re: QDirModel - add own items

    ok now i subclassed QDirModel::data and QDirModel::rowCount as following:

    Qt Code:
    1. int MyDirModel::rowCount(const QModelIndex & parent)const
    2. {
    3. if(parent == QModelIndex())
    4. return QDirModel::rowCount(parent) + 1;
    5.  
    6. return QDirModel::rowCount(parent);
    7. }
    8.  
    9. QVariant MyDirModel::data(const QModelIndex & index, int role)const
    10. {
    11. if(index.row() == (QDirModel::rowCount(QModelIndex()) + 1))
    12. {
    13. if(role == Qt::DisplayRole)
    14. return QString(tr("bla"));
    15.  
    16. if(role == Qt::DecorationRole)
    17. return iconProvider()->icon(QFileIconProvider::Desktop);
    18.  
    19. return QVariant();
    20. }
    21.  
    22. return QDirModel::data(index, role);
    23. }
    To copy to clipboard, switch view to plain text mode 

    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 ((
    Last edited by heinz32; 11th May 2008 at 17:02.

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QDirModel - add own items

    Qt Code:
    1. if(index.row() == (QDirModel::rowCount(QModelIndex()) + 1))
    To copy to clipboard, switch view to plain text mode 

    should be:

    Qt Code:
    1. if(index.row() == QDirModel::rowCount(QModelIndex()))
    To copy to clipboard, switch view to plain text mode 

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

  8. #8

    Default Re: QDirModel - add own items

    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

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QDirModel - add own items

    Because you should check the parent as well

  10. #10

    Default Re: QDirModel - add own items

    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?
    Last edited by heinz32; 11th May 2008 at 22:50.

  11. #11

    Default Re: QDirModel - add own items

    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

Similar Threads

  1. Arranging the Items in Qtopia
    By sar_van81 in forum Qt for Embedded and Mobile
    Replies: 3
    Last Post: 10th December 2007, 08:52
  2. Edit items on QTreeView + QDirModel
    By junior0007 in forum Qt Programming
    Replies: 4
    Last Post: 23rd November 2007, 07:16
  3. Light items for the graphicsView
    By maverick_pol in forum Qt Programming
    Replies: 12
    Last Post: 1st November 2007, 18:51
  4. Replies: 1
    Last Post: 15th March 2007, 20:45
  5. Selective highlighting of Items
    By Kapil in forum Qt Programming
    Replies: 3
    Last Post: 26th May 2006, 12:20

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.