Results 1 to 6 of 6

Thread: QAbstractProxyModel and QTreeView

Threaded View

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

    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

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.