Results 1 to 2 of 2

Thread: QAbstractItemModel for dummies

  1. #1
    Join Date
    May 2006
    Posts
    9
    Thanks
    1

    Default QAbstractItemModel for dummies

    Hi,

    I have been trying to understand how to subclass QAbstractItemModel and create data for it but cannot get my head around it.

    I have looked through Qt's own documentation (many times) and examples like Simple Tree Model Example. I can get the examples to work but cannot quite understand how it works.

    Can someone please help me with links to examples with in dept description of how it all works step by step (not Qt's own examples - tried that!).

    Morty.

  2. #2
    Join Date
    Aug 2006
    Posts
    163
    Thanks
    12
    Thanked 5 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QAbstractItemModel for dummies

    It is very confusing if you've never used it before. Basically, what helped me to understand it the most it that QModelIndex holds the column and row coordinates relative to the parent of any given item. For root items, the model index is the model index of the tree/view/widget itself, which is, if I understand correctly, a model index with the values 0,0. Or something close to that anyways.
    When you add items, you have to pass them the correct parent QModelItem so that Qt can then fill in the correct values for the child you've just added.

    When you subclass QAbstractItemModel, you have to implement a lot of the basic functionality yourself. I just made a simple one that adds the values from an array of QStrings into the right columns upon creation, the code may help you to understand how it works. Passing the model after it's been created to the
    QTreeView as it's new model gives the desired view.

    Qt Code:
    1. /***************************************************************************
    2. * Copyright (C) 2006 by Lawrence Lee *
    3. * valheru@facticius.net *
    4. * *
    5. * This program is free software; you can redistribute it and/or modify *
    6. * it under the terms of the GNU General Public License as published by *
    7. * the Free Software Foundation; either version 2 of the License, or *
    8. * (at your option) any later version. *
    9. * *
    10. * This program is distributed in the hope that it will be useful, *
    11. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
    12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
    13. * GNU General Public License for more details. *
    14. * *
    15. * You should have received a copy of the GNU General Public License *
    16. * along with this program; if not, write to the *
    17. * Free Software Foundation, Inc., *
    18. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
    19. ***************************************************************************/
    20. #include "modelItem.h"
    21. #include "articleModel.h"
    22. #include <QtCore/QDebug>
    23. #include <QtCore/QStringList>
    24.  
    25. ArticleModel::ArticleModel( const QStringList &data, QObject *parent ) : QAbstractItemModel( parent )
    26. {
    27. QList<QString> rootData;
    28. rootData << "From" << "Subject" << "Date" << "Message-ID"<< "Lines";
    29. rootItem = new ModelItem( rootData );
    30. for ( int i = 0; i < data.count(); i += 5 ) {
    31. ModelItem *item = new ModelItem( QStringList() << data[ i ] << data[ i + 1 ] << data[ i + 2 ] << data[ i + 3 ] << data[ i + 4 ], rootItem );
    32. rootItem->appendChild( item );
    33. }
    34. }
    35.  
    36. ArticleModel::~ArticleModel()
    37. {
    38. delete rootItem;
    39. }
    40.  
    41. QModelIndex ArticleModel::index( int row, int column, const QModelIndex &parent ) const
    42. {
    43. ModelItem * parentItem;
    44.  
    45. if ( !parent.isValid() )
    46. parentItem = rootItem;
    47. else
    48. parentItem = static_cast<ModelItem*>( parent.internalPointer() );
    49.  
    50. ModelItem *childItem = parentItem->child( row );
    51. if ( childItem )
    52. return createIndex( row, column, childItem );
    53. else
    54. return QModelIndex();
    55. }
    56.  
    57. QModelIndex ArticleModel::parent( const QModelIndex &index ) const
    58. {
    59. if ( !index.isValid() )
    60. return QModelIndex();
    61.  
    62. ModelItem *childItem = static_cast<ModelItem*>( index.internalPointer() );
    63. ModelItem *parentItem = childItem->parent();
    64.  
    65. if ( parentItem == rootItem )
    66. return QModelIndex();
    67.  
    68. return createIndex( parentItem->row(), 0, parentItem );
    69. }
    70.  
    71. int ArticleModel::rowCount( const QModelIndex &parent ) const
    72. {
    73. ModelItem * parentItem;
    74.  
    75. if ( !parent.isValid() )
    76. parentItem = rootItem;
    77. else
    78. parentItem = static_cast<ModelItem*>( parent.internalPointer() );
    79.  
    80. return parentItem->childCount();
    81. }
    82.  
    83. int ArticleModel::columnCount( const QModelIndex &parent ) const
    84. {
    85. if ( parent.isValid() )
    86. return static_cast<ModelItem*>( parent.internalPointer() ) ->columnCount();
    87. else
    88. return rootItem->columnCount();
    89. }
    90.  
    91. QVariant ArticleModel::data( const QModelIndex &index, int role ) const
    92. {
    93. if ( !index.isValid() )
    94. return QVariant();
    95.  
    96. if ( role != Qt::DisplayRole )
    97. return QVariant();
    98.  
    99. ModelItem *item = static_cast<ModelItem*>( index.internalPointer() );
    100.  
    101. return item->data( index.column() );
    102. }
    103.  
    104. Qt::ItemFlags ArticleModel::flags( const QModelIndex &index ) const
    105. {
    106. if ( !index.isValid() )
    107. return Qt::ItemIsEnabled;
    108.  
    109. return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
    110. }
    111.  
    112. QVariant ArticleModel::headerData( int section, Qt::Orientation orientation, int role ) const
    113. {
    114. if ( orientation == Qt::Horizontal && role == Qt::DisplayRole )
    115. return rootItem->data( section );
    116.  
    117. return QVariant();
    118. }
    To copy to clipboard, switch view to plain text mode 

  3. The following 2 users say thank you to Valheru for this useful post:

    morty (12th October 2006), neuronet (21st July 2014)

Similar Threads

  1. Subclassing QAbstractItemModel
    By r366y in forum Qt Programming
    Replies: 2
    Last Post: 2nd May 2006, 09:46
  2. [QT4] QTreeView, QAbstractItemModel and sorting
    By KShots in forum Qt Programming
    Replies: 3
    Last Post: 24th March 2006, 21:16
  3. Replies: 8
    Last Post: 7th March 2006, 14:40

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.