Results 1 to 11 of 11

Thread: QDirModel shared in two TreeViews with different TreeView configurations...

  1. #1
    Join Date
    Aug 2008
    Posts
    39
    Thanks
    12
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question QDirModel shared in two TreeViews with different TreeView configurations...

    I've been looking at examples and reading docs etc. I'd like run my thoughts by someone on how this would work, because I haven't run across this scenario anywhere.

    Problem:

    I would like to use a custom QDirModel for two TreeViews in one splitter pane, but I'd like to customize the look of one vs the other. currently doing via two QDirModels...
    So on the left side I would like just folders and the name column (in TreeView form)
    On the right I'd like the full up TreeView.

    So would I implement this by using a QAbstractItemModel? If, yes how would this be accomplished? If no how can I use one custom QDirModel in two TreeViews with different layouts? What are your thoughts?

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

    Default Re: QDirModel shared in two TreeViews with different TreeView configurations...

    Hide all the columns but the first one in the tree. If you want to filter out files, use QSortFilterProxyModel and in its filterAcceptsRow() check if the index is a file or a directory and return false or true respectively. Then you'll be able to use one model for both views (one of them will additionaly be using a proxy model).

  3. The following user says thank you to wysota for this useful post:

    killerwookie99 (15th August 2008)

  4. #3
    Join Date
    Aug 2008
    Posts
    39
    Thanks
    12
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QDirModel shared in two TreeViews with different TreeView configurations...

    What about QProxyModel? It says not to use it, is there something better?
    Last edited by killerwookie99; 15th August 2008 at 00:31.

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

    Default Re: QDirModel shared in two TreeViews with different TreeView configurations...

    Yes, you have a link to the docs in my previous post.

  6. The following user says thank you to wysota for this useful post:

    killerwookie99 (15th August 2008)

  7. #5
    Join Date
    Aug 2008
    Posts
    39
    Thanks
    12
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QDirModel shared in two TreeViews with different TreeView configurations...

    Alright I'll stick with it, its use is just a little confusing for me.
    Last edited by killerwookie99; 15th August 2008 at 17:01.

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

    Default Re: QDirModel shared in two TreeViews with different TreeView configurations...

    What is confusing? The difference between QProxyModeland QAbstractProxyModel which is the base of QSortFilterProxyModel?

  9. #7
    Join Date
    Aug 2008
    Posts
    39
    Thanks
    12
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QDirModel shared in two TreeViews with different TreeView configurations...

    When I try to do this it segfaults and I'm not sure if it's because I'm missing a function or what, but here's a simplistic version of what I tried to do...oh and it segfaults even when I comment out filterAcceptsRow...

    MySortFilterProxyModel.h
    Qt Code:
    1. #include <QtGui/QSortFilterProxyModel>
    2.  
    3. class MySortFilterProxyModel : public QSortFilterProxyModel
    4. {
    5. Q_OBJECT
    6. public:
    7. MySortFilterProxyModel(QObject *parent = 0);
    8.  
    9. protected:
    10. bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const;
    11. };
    To copy to clipboard, switch view to plain text mode 

    MySortFilterProxyModel.cpp
    Qt Code:
    1. #include "MySortFilterProxyModel.h"
    2.  
    3. #include <iostream>
    4.  
    5. MySortFilterProxyModel::MySortFilterProxyModel(QObject *parent) : QSortFilterProxyModel(parent)
    6. {
    7.  
    8. }
    9.  
    10. bool MySortFilterProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
    11. {
    12. QModelIndex index0 = sourceModel()->index(sourceRow, 0, sourceParent);
    13.  
    14. std::cout << "Index: " << sourceModel()->data(index0).toString().toStdString() << std::endl;
    15.  
    16. return QSortFilterProxyModel::filterAcceptsRow(sourceRow, sourceParent);
    17. }
    To copy to clipboard, switch view to plain text mode 

    MainWindow.cpp
    ...
    Qt Code:
    1. QDirModel *dirModel = new QDirModel();
    2. MySortFilterProxyModel *proxyModel = new MySortFilterProxyModel();
    3.  
    4. proxyModel->setSourceModel(dirModel); // SEGFAULTS
    To copy to clipboard, switch view to plain text mode 
    ...

    Any ideas?

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

    Default Re: QDirModel shared in two TreeViews with different TreeView configurations...

    What is the source model? Is it a custom model or a predefined one?

  11. #9
    Join Date
    Aug 2008
    Posts
    39
    Thanks
    12
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QDirModel shared in two TreeViews with different TreeView configurations...

    It's using the regular qdirmodel from qt...

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

    Default Re: QDirModel shared in two TreeViews with different TreeView configurations...

    Could you print the backtrace from the debugger upon the segfault?

  13. #11
    Join Date
    Aug 2008
    Posts
    39
    Thanks
    12
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QDirModel shared in two TreeViews with different TreeView configurations...

    You want a truss or something different, sorry my debugger knowledge is limited...

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.