Results 1 to 18 of 18

Thread: data, model and tree view

  1. #1
    Join Date
    Jun 2006
    Location
    San Jose, CA
    Posts
    53
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default data, model and tree view

    Hi,
    I have a maybe stupid question but, can anyone help me here:

    I have a data class which gets manipulated through a tableWidget. Then I have a model and a treeview based on that data. The data can only be viewed in the tree view. How do I tell the model that the data was changed through the table widget?

    The table widget looks like that:
    a1 1 2 3
    b1 4 5

    The tree view displays the full factorial combinations of a1 b1

    Qt Code:
    1. a1
    2. 1
    3. b1
    4. 4
    5. 5
    6. 2
    7. b1
    8. 4
    9. 5
    10. 3
    11. b1
    12. 4
    13. 5
    To copy to clipboard, switch view to plain text mode 

    So, adding a value to b1 would change the tree. I guess (at least I don't see how) I can't have a single model for the two different views since the setData is different for both views.
    I appreciate any comments to help me solve my problem.
    Thanks.
    Last edited by jacek; 24th June 2006 at 12:29. Reason: added code tags to make the tree structure visible

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

    Default Re: data, model and tree view

    Why do you say that you need two different models? If you insist on that (I don't say you shouldn't), use a proxy model (subclass either the abstract proxy model class or its sort/filter subclass) and implement those "versions" there using a common base. Thanks to that all three or two models (core+two proxies or core+one proxy) will always be in sync on their own.

  3. #3
    Join Date
    Jun 2006
    Location
    San Jose, CA
    Posts
    53
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: data, model and tree view

    I was thinking to use two models since the two data representations have not nuch in comon.
    The
    a1 1 2 3
    b1 4 5
    is just the summary of the factors used in the full factorial representation so, it would be natural to keep them seperate. If I choose your way of having a single model and two proxies I'm struggling to find a nice interface to serve both views. Also it's not clear to me how to use the proxies since the documentation is not very good. Any suggestion where to finde more detailed info's on proxy and mybe some examples.
    Thanks a lot.

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

    Default Re: data, model and tree view

    IMO the model is the same in both cases, just the hierarchy differs, that's why I suggest to use a proxy -- to change the hierarchy. You might want to take a look at the source of QSortFilterProxyModel. In general you need to reimplement methods responsible for mapping indexes from the source model to the proxy model and vice versa.

  5. #5
    Join Date
    Jun 2006
    Location
    San Jose, CA
    Posts
    53
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: data, model and tree view

    I mananged to create a proxy model which is currently just doing the same as the model itself.

    treeview
    |
    proxy tableview
    | |
    model
    |
    data

    Playing with it I noticed that if I change data in the tablview the treeview is not updated. Do I miss something here?
    Thanks.

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

    Default Re: data, model and tree view

    Can we see some code? Hard to say anything without it.

  7. #7
    Join Date
    Jun 2006
    Location
    San Jose, CA
    Posts
    53
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: data, model and tree view

    Sure, I attached the proxyFilter code. What's also interessting that if I click into the treeview I get a bunch of cout's from the setData instead just a single one.

    Qt Code:
    1. snip main.cpp
    2.  
    3. TestModel model;
    4. QSplitter *splitter = new QSplitter;
    5. QTreeView *tree = new QTreeView(splitter);
    6. QTableView *table = new QTableView(splitter);
    7.  
    8. table->setModel(&model);
    9. table->setRootIndex(QModelIndex());
    10.  
    11. ProxyFilter filterModel;
    12. filterModel.setSourceModel(&model);
    13. tree->setModel(&filterModel);
    14.  
    15. splitter->setWindowTitle("Test views");
    16. splitter->show();
    17.  
    18. return app.exec();
    19. }
    To copy to clipboard, switch view to plain text mode 

    proxyFilter.cpp:
    Qt Code:
    1. #include "proxyFilter.h"
    2. #include <iostream>
    3.  
    4. ProxyFilter::ProxyFilter() {
    5. }
    6.  
    7. ProxyFilter::~ProxyFilter() {
    8. }
    9.  
    10. QModelIndex ProxyFilter::mapFromSource (const QModelIndex & sourceIndex) const {
    11.  
    12. std::cout << "mapFromSource" << std::endl;
    13.  
    14. if(sourceIndex.isValid())
    15. return createIndex(sourceIndex.row(), sourceIndex.column());
    16. else
    17. return QModelIndex();
    18. }
    19.  
    20. QModelIndex ProxyFilter::mapToSource (const QModelIndex & proxyIndex) const {
    21.  
    22. std::cout << "mapToSource" << std::endl;
    23. return sourceModel()->index(proxyIndex.row(), proxyIndex.column());
    24. }
    25.  
    26.  
    27. int ProxyFilter::columnCount(const QModelIndex & parent) const {
    28.  
    29. // return sourceModel()->columnCount(parent);
    30.  
    31. // filter out all cols - keep only col = 0
    32. return 1;
    33.  
    34. }
    35.  
    36. int ProxyFilter::rowCount(const QModelIndex & parent) const {
    37. return sourceModel()->rowCount(parent);
    38. }
    39.  
    40.  
    41. QModelIndex ProxyFilter::index(int row, int column, const QModelIndex& parent) const {
    42.  
    43. return sourceModel()->index(row, column, parent);
    44.  
    45. }
    46.  
    47. QModelIndex ProxyFilter::parent(const QModelIndex&) const {
    48.  
    49. return QModelIndex();
    50. }
    51.  
    52. QVariant ProxyFilter::data(const QModelIndex & index, int role) const {
    53.  
    54. std::cout << "data: row =" << index.row() << " col=" << index.column() << std::endl;
    55. return sourceModel()->data(index, role);
    56.  
    57. }
    58.  
    59. bool ProxyFilter::setData(const QModelIndex &index, const QVariant &value, int role) {
    60.  
    61. std::cout << "setData: row =" << index.row() << " col=" << index.column() << std::endl;
    62. return sourceModel()->setData(index, value, role);
    63.  
    64. }
    65.  
    66. Qt::ItemFlags ProxyFilter::flags(const QModelIndex &index) const {
    67.  
    68. return sourceModel()->flags(index);
    69.  
    70. }
    71.  
    72. void ProxyFilter::setSourceModel(QAbstractItemModel *sourceModel) {
    73.  
    74. connect(sourceModel, SIGNAL(modelReset()), this, SIGNAL(modelReset()));
    75. QAbstractProxyModel::setSourceModel(sourceModel);
    76.  
    77. }
    To copy to clipboard, switch view to plain text mode 

    proxyFilter.h:
    Qt Code:
    1. #ifndef PROXYFILTER_H
    2. #define PROXYFILTER_H
    3.  
    4. #include <QAbstractProxyModel>
    5. #include <QAbstractItemModel>
    6.  
    7. class ProxyFilter : public QAbstractProxyModel {
    8.  
    9. Q_OBJECT
    10.  
    11. public:
    12.  
    13. ProxyFilter();
    14. ~ProxyFilter();
    15.  
    16. int columnCount(const QModelIndex & parent = QModelIndex()) const;
    17. int rowCount(const QModelIndex & parent = QModelIndex()) const;
    18.  
    19. QModelIndex mapFromSource (const QModelIndex & sourceIndex) const;
    20. QModelIndex mapToSource (const QModelIndex & proxyIndex) const;
    21.  
    22. QModelIndex index(int row, int column, const QModelIndex& parent = QModelIndex()) const;
    23. QModelIndex parent(const QModelIndex&) const ;
    24.  
    25. QVariant data(const QModelIndex & index, int role) const;
    26. bool setData (const QModelIndex &index, const QVariant &value, int role);
    27.  
    28. Qt::ItemFlags flags(const QModelIndex &index) const;
    29.  
    30. void setSourceModel(QAbstractItemModel *sourceModel);
    31.  
    32. };
    33.  
    34. #endif
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    Jun 2006
    Location
    San Jose, CA
    Posts
    53
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: data, model and tree view

    Well, it's actually data cout's I get when just moving the mouse over the field - strange?!?

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

    Default Re: data, model and tree view

    No, it's not strange Anyway, everytime you use indexes with the source model, you have to map them to the source model space using mapToSource(). And everytime where you get an index from the source model, you should map it to the proxy model space using mapFromSource.

    As for multple occurences of data() calls -- don't worry about it, it's perfectly normal. The view is asking the model for things like a tooltip for the item under the pointer. If you extend your cout to also show the roles in question, you'll see that for yourself.

  10. #10
    Join Date
    Jun 2006
    Location
    San Jose, CA
    Posts
    53
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: data, model and tree view

    You mean instead of using (e.g. in data)
    return sourceModel()->data(index, role);

    I should better use
    return sourceModel()->data(mapToSource(index), role);

    That make the code cleaner

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

    Default Re: data, model and tree view

    Quote Originally Posted by larry104
    You mean instead of using (e.g. in data)
    return sourceModel()->data(index, role);

    I should better use
    return sourceModel()->data(mapToSource(index), role);
    Yes. And the same in all other places. Remember that indexes are only valid in context of a current model. You can't "share" or "move" indexes between models. And you can't store them too (you have QPersistentModelIndex for that).

  12. #12
    Join Date
    Jun 2006
    Location
    San Jose, CA
    Posts
    53
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: data, model and tree view

    Ok. I still have the problem that the data changed in the table is not updated in the tree view. If I click into the tree it will get of cause updated since it get's it from the model. Any idea why the views are not in sync? I should mention that if the data is changed in the treeview is updated in the tableview.

  13. #13
    Join Date
    Jun 2006
    Location
    San Jose, CA
    Posts
    53
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: data, model and tree view

    I found a solution to the problem I have. I needed to connect the SIGNAL form the model to a custom slot of my PROXY which then emits a dataChanged. So, the modified setSourceModel looks like:

    Qt Code:
    1. void ProxyFilter::setSourceModel(QAbstractItemModel *sourceModel) {
    2.  
    3. connect(sourceModel, SIGNAL(modelReset()), this, SIGNAL(modelReset()));
    4. QAbstractProxyModel::setSourceModel(sourceModel);
    5.  
    6. if (sourceModel) {
    7. connect(sourceModel, SIGNAL(dataChanged(QModelIndex,QModelIndex)),
    8. this, SLOT(_my_sourceDataChanged(QModelIndex,QModelIndex)));
    9. }
    10.  
    11. }
    To copy to clipboard, switch view to plain text mode 

    and the implementation of the SLOT _my_sourceDataChanged

    Qt Code:
    1. QModelIndex ProxyFilter::_my_sourceDataChanged(const QModelIndex &source_top_left, const QModelIndex &source_bottom_right) {
    2.  
    3. QModelIndex proxy_top_left = mapFromSource(source_top_left);
    4. QModelIndex proxy_bottom_right = mapFromSource(source_bottom_right);
    5. emit dataChanged(proxy_top_left, proxy_bottom_right);
    6. }
    To copy to clipboard, switch view to plain text mode 

    Was this really necesarry to do? Shouldn't it be the idea of a proxy to exactly do this automatically? Any comments - did I miss something?
    Thanks.

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

    Default Re: data, model and tree view

    Yes, it should handle it on its own. It might be a bug. Are you using an up to date release of Qt?

  15. #15
    Join Date
    Jun 2006
    Location
    San Jose, CA
    Posts
    53
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: data, model and tree view

    Yep, I'm using 4.1.3. Has anyone else noticed that problem?

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

    Default Re: data, model and tree view

    4.1.4 is the newest one. Maybe the bug has been reported? Have you checked the task-tracker on the Trolltech site?

  17. #17
    Join Date
    Jun 2006
    Location
    San Jose, CA
    Posts
    53
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: data, model and tree view

    I installed 4.1.4 - same problem.

  18. #18
    Join Date
    May 2006
    Posts
    23
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: data, model and tree view

    I noticed the same problem as well. No signals are propagated when deriving from a QAbstractProxyModel. QSortFilterProxyModel propagates signals just fine.

Similar Threads

  1. Drag and drop items in view to sort order
    By Big Duck in forum Qt Programming
    Replies: 1
    Last Post: 25th May 2006, 19:43
  2. Replies: 1
    Last Post: 1st March 2006, 11:43

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.