I'm developing an binary news reader. In the end I'm using an sqlite database to store the data in, but I'm first trying to get to grips with the model/view structure by using a simple QStringList to hold the data.
I'm subclassing the QAbstractItemModel to achieve all this, but I'm a bit lost. I did this once before in Java for a school assignment, but I can't remember much of it. At any rate, Qt's way of doing it has got me stumped.
I don't really understand what I'm doing, despite having read the relavent documentation and looking at the examples. What I have achieved works, except anything added to the string list doesn't show up in the tree view. It is added to the array, as you can see in the code I checked that using a QMessageBox.

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 <QHeaderView>
  21. #include <QMessageBox>
  22. #include "serverListModel.h"
  23.  
  24. serverListModel::serverListModel( QObject *parent )
  25. {
  26. p = parent;
  27. }
  28.  
  29. serverListModel::~serverListModel()
  30. {
  31. }
  32.  
  33. QModelIndex serverListModel::index( int row, int column, const QModelIndex &parent ) const
  34. {
  35. return QModelIndex();
  36. }
  37.  
  38. QModelIndex serverListModel::parent( const QModelIndex &index ) const
  39. {
  40. return QModelIndex();
  41. }
  42.  
  43. int serverListModel::rowCount( const QModelIndex &parent ) const
  44. {
  45. return list.count();
  46. }
  47.  
  48. int serverListModel::columnCount( const QModelIndex &parent ) const
  49. {
  50. if( p->objectName() == "serverTreeList" ){
  51. return 1;
  52. }
  53. if( p->objectName() == "articleTreeList" ){
  54. return 3;
  55. }
  56. }
  57.  
  58. QVariant serverListModel::data( const QModelIndex &index, int role ) const
  59. {
  60. return list.at( index.row() );
  61. }
  62.  
  63. QVariant serverListModel::headerData( int section, Qt::Orientation orientation, int role ) const
  64. {
  65. if( role != Qt::DisplayRole ){
  66. return QVariant();
  67. }else{
  68. if( p->objectName() == "serverTreeList" ){
  69. return QString( "Server List" );
  70. }
  71. if( p->objectName() == "articleTreeList" ){
  72. return QString( "Article List" );
  73. }
  74. }
  75. }
  76.  
  77. Qt::ItemFlags serverListModel::flags( const QModelIndex &index ) const
  78. {
  79. return Qt::ItemIsEnabled;
  80. }
  81.  
  82.  
  83. bool serverListModel::setData( const QModelIndex &index, const QVariant &value, int role )
  84. {
  85. list.replace(index.row(), value.toString());
  86. emit dataChanged(index, index);
  87. return true;
  88. }
  89.  
  90. bool serverListModel::insertRows( int position, int rows, const QModelIndex &parent )
  91. {
  92. beginInsertRows( QModelIndex(), position, position + rows - 1 );
  93. for( int row = 0; row < rows; ++row ){
  94. list.insert(position, "");
  95. }
  96. endInsertRows();
  97. return true;
  98. }
  99.  
  100. bool serverListModel::removeRows( int position, int rows, const QModelIndex &parent )
  101. {
  102. beginRemoveRows( QModelIndex(), position, position + rows - 1 );
  103. for( int row = 0; row < rows; ++row ){
  104. list.removeAt(position);
  105. }
  106. endRemoveRows();
  107. return true;
  108. }
  109.  
  110. void serverListModel::addItem( const QString& item )
  111. {
  112. QModelIndex index;
  113. beginInsertRows( index, 1, 1 );
  114. list.insert(list.size(), item);
  115. endInsertRows();
  116. for( int i = 0; i < list.size(); i++ ){
  117. QMessageBox *msg = new QMessageBox( "list", "list[" + QString::number(i) + "] = " + list[i],
  118. QMessageBox::Information, QMessageBox::Ok, 0, 0 );
  119. msg->exec();
  120. }
  121. }
To copy to clipboard, switch view to plain text mode 

If someone could take the time to explain how the whole model/view thing works wtih regards to the QModelIndex that passed to all the functions you have to reimplement I'd be much obliged, but I'd settle for knowing why my tree view doesn't reflect the changes made to the string list