Results 1 to 4 of 4

Thread: QAbstractProxyModel tutorial? or advice?

  1. #1
    Join Date
    Sep 2009
    Posts
    57
    Thanks
    7
    Thanked 5 Times in 4 Posts

    Question QAbstractProxyModel tutorial? or advice?

    Hi,

    I got all excited when I saw this: http://labs.trolltech.com/blogs/2008...-of-modelview/ then found the source link is broken :-(

    I have a QSqlTableModel which is being displayed in a QTreeView. It works nicely.

    I want to allow the user to 'groupBy' a particular column, generally a text field.

    So as a first stab I wrote a QAbstractProxyModel to just pass things straight thru. I got it largely working except the SQLTableModel implements canFetchMore which I had to muck about with in rowCount and then all my troubles began.

    I could share code and get help that way, but before that -- is there a good tutorial anywhere that will help me? I am particularly interested in understanding how createIndex is supposed to work and why it doesn't accept a parent parameter, even tho QModelIndex needs one....

    Anyway thanks for any pointers you can give me with this.

    Cheers,
    Mark

  2. #2
    Join Date
    Sep 2009
    Posts
    57
    Thanks
    7
    Thanked 5 Times in 4 Posts

    Default Re: QAbstractProxyModel tutorial? or advice?

    Perhaps it would be better to share my code.

    The following works ok for me. In my main application I create a QSQLTableModel, do a select() on it and then while it canFetchMore I fetchMore (shouldn't the view do this?).

    Then I create my GroupByModel and setSourceModel to the QSQLTableModel before then adding a QSortFilterProxyModel and setting its source model to my GroupByModel and then, finally, setModel on my QTableView to the QSortFilterProxyModel.

    Is this code "correct" as a pass-through proxy (i.e. it doesn't do anything at all at the moment, just reflects the source model).

    I'm about to start modifying it to create a mapping from the sqlmodel and would like to be sure I'm not doing things incorrectly....

    Any advice gratefully received....

    Qt Code:
    1. // Proxy model ... transparent at the moment
    2. class GroupByModel : public QAbstractProxyModel
    3. {
    4. Q_OBJECT
    5.  
    6. private:
    7. bool *groupBy;
    8.  
    9. public:
    10.  
    11. GroupByModel(bool *groupBy, QObject *parent = NULL) : QAbstractProxyModel(parent), groupBy(groupBy) {
    12. setParent(parent);
    13. }
    14. ~GroupByModel() {}
    15.  
    16. // create a model index, don't bother with parent for now
    17. // since -1,-1 has no parent and all others have a parent of -1.-1
    18. QModelIndex index(int row, int column, const QModelIndex & = QModelIndex()) const {
    19. return createIndex(row,column);
    20. }
    21.  
    22. // parent is at -1, -1. All others have a parent of -1, -1
    23. QModelIndex parent(const QModelIndex &index) const {
    24. // we are at the top, return an invalid parent
    25. if (index.column() == -1 && index.row() == -1) return QModelIndex();
    26. else return createIndex(-1,-1);
    27. }
    28.  
    29. QModelIndex mapToSource(const QModelIndex &proxyIndex) const {
    30.  
    31. // we use the same co-ords for now, so map to the same
    32. return sourceModel()->index(proxyIndex.row(),proxyIndex.column());
    33. }
    34.  
    35. // source model should be the same as ours
    36. QModelIndex mapFromSource(const QModelIndex &sourceIndex) const {
    37. return index(sourceIndex.row(), sourceIndex.column());
    38. }
    39.  
    40. // same columns as source please
    41. int columnCount(const QModelIndex &parent = QModelIndex()) const {
    42. return sourceModel()->columnCount(sourceModel()->index(parent.row(), parent.column()));
    43. }
    44.  
    45. int rowCount(const QModelIndex &parent = QModelIndex()) const {
    46. if (parent.row() == -1 && parent.column() == -1) {
    47. return sourceModel()->rowCount(sourceModel()->index(parent.row(), parent.column()));
    48. } else
    49. return 0;
    50. }
    51.  
    52. // does this index have children?
    53. bool hasChildren(const QModelIndex &index) const {
    54. // if it is the root index (-1,-1) - it might have children
    55. if (index.row() == -1 && index.column() == -1 && rowCount(createIndex(-1,-1)))
    56. return true;
    57. else
    58. return false;
    59. }
    60.  
    61. };
    To copy to clipboard, switch view to plain text mode 
    Last edited by liversedge; 1st August 2010 at 19:43. Reason: comments in the code were out of sync with the code!

  3. #3
    Join Date
    Sep 2009
    Posts
    57
    Thanks
    7
    Thanked 5 Times in 4 Posts

    Default Re: QAbstractProxyModel tutorial? or advice?


  4. #4
    Join Date
    Sep 2009
    Posts
    57
    Thanks
    7
    Thanked 5 Times in 4 Posts

    Default Re: QAbstractProxyModel tutorial? or advice?

    http://gitorious.org/qtgroupingproxy/qtgroupingproxy

    For future reference, in case someone stumbles across this thread, the kde guys have created a grouping proxy, it is at the url above.

Similar Threads

  1. How to subclass QAbstractProxyModel
    By skycrestway in forum Qt Programming
    Replies: 2
    Last Post: 23rd October 2010, 00:35
  2. QAbstractProxyModel to do a Tree
    By xgoan in forum Qt Programming
    Replies: 3
    Last Post: 18th November 2008, 18:31
  3. QAbstractProxyModel lessThan [SOLVED]
    By killerwookie99 in forum Qt Programming
    Replies: 3
    Last Post: 15th September 2008, 20:42
  4. Help with QAbstractProxyModel
    By killerwookie99 in forum Qt Programming
    Replies: 9
    Last Post: 12th September 2008, 23:13
  5. QAbstractProxyModel and QTreeView
    By niko in forum Qt Programming
    Replies: 5
    Last Post: 18th January 2008, 22:17

Tags for this Thread

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.