Results 1 to 6 of 6

Thread: QAbstractProxyModel and QTreeView

  1. #1
    Join Date
    Jan 2006
    Posts
    105
    Thanks
    21
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QAbstractProxyModel and QTreeView

    I want to create a Proxy Model - as a first step I tried to create a Proxy that should behave exaclty like the sourceModel:
    Qt Code:
    1. class Proxy : public QAbstractProxyModel
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6.  
    7. Proxy(QObject* parent = 0) : QAbstractProxyModel(parent) { }
    8.  
    9. QModelIndex mapFromSource ( const QModelIndex & sourceIndex ) const
    10. {
    11. return sourceIndex;
    12. }
    13.  
    14. QModelIndex mapToSource ( const QModelIndex & proxyIndex ) const
    15. {
    16. return proxyIndex;
    17. }
    18.  
    19. int rowCount(const QModelIndex& parent) const
    20. {
    21. return sourceModel()->rowCount(parent);
    22. }
    23.  
    24. int columnCount(const QModelIndex& parent) const
    25. {
    26. return sourceModel()->columnCount(parent);
    27. }
    28.  
    29. QModelIndex index(int row, int col, const QModelIndex& parent) const
    30. {
    31. return sourceModel()->index(row, col, parent);
    32. }
    33. QModelIndex parent(const QModelIndex& index) const
    34. {
    35. return parent(index);
    36. }
    37. };
    To copy to clipboard, switch view to plain text mode 
    So far one thing is unclear to me: why do I have to implement rowCount, columnCount, index and parent? Why isn't this done in QAbstractProxyModel?

    when using my proxy with the simpletreemodel-example this way:
    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. Q_INIT_RESOURCE(simpletreemodel);
    4.  
    5. QApplication app(argc, argv);
    6.  
    7. QFile file(":/default.txt");
    8. file.open(QIODevice::ReadOnly);
    9. TreeModel model(file.readAll());
    10. file.close();
    11.  
    12. Proxy proxy;
    13. proxy.setSourceModel(&model);
    14.  
    15. QTreeView view;
    16. view.setModel(&proxy);
    17. view.setWindowTitle(QObject::tr("Simple Tree Model"));
    18. view.show();
    19. return app.exec();
    20. }
    To copy to clipboard, switch view to plain text mode 
    I get a messed-up tree - see attached screenshot.
    Using a QTableView however works!

    please help
    niko
    Attached Images Attached Images

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QAbstractProxyModel and QTreeView

    What do you intend to do with the proxy model?
    J-P Nurmi

  3. #3
    Join Date
    Jan 2006
    Posts
    105
    Thanks
    21
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QAbstractProxyModel and QTreeView

    I'm writing a KDeveop4 plugin.

    I have an existing model (the ProjectModel) and need one that displays all project-items just as the ProjectModel and makes the items checkable. So I wanted to re-implement flags(), setData() and data() - where I handle myselfe the checkbox-stuff - and forward the rest to the ProjectModel.

    Or is there a better way?

    thanks,
    niko

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QAbstractProxyModel and QTreeView

    You could simply use QSortFilterProxyModel which saves you from writing any of those methods. Just reimplement flags() and you're done.
    J-P Nurmi

  5. The following user says thank you to jpn for this useful post:

    niko (18th January 2008)

  6. #5
    Join Date
    Jan 2006
    Posts
    105
    Thanks
    21
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QAbstractProxyModel and QTreeView

    thanks, QSortFilterProxyModel works.

    However I don't understand why my proxy didn't...

  7. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QAbstractProxyModel and QTreeView

    Notice that every QModelIndex belongs to a certain model. The view doesn't know there's proxy model in between. Besides other possible logical mistakes, the view only sees a model which returns indexes belonging to some other unknown model.
    J-P Nurmi

  8. The following user says thank you to jpn for this useful post:

    niko (18th January 2008)

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.