Results 1 to 6 of 6

Thread: Need help with QTreeView/QAbstractItemModel

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

    Default Need help with QTreeView/QAbstractItemModel

    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

  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: Need help with QTreeView/QAbstractItemModel

    You are doing many major errors here. I even started going through them explaining but then my browser crashed and I don't want to write that all again.

    In short words:

    • you can't rely on the view in the model, the model is just a data representation, it shouldn't be tied to a view
    • if you want two different sets of data with different column count, use two separate instances of the model and make it possible for the model to operate on different number of columns
    • subclass a proper model, in your situation using QAbstractTableModel would be much better
    • you can't return invalid indexes only from the index() method as the whole model-view architecture relies on it, you should use createIndex() to return proper indexes
    • take a look at model examples provided with Qt, maybe it will put some more light on the subject
    • why don't you just use QStringListModel or QStandardItemModel instead of making an own model class? They exactly fit your needs.
    • the view doesn't show any changes probably because you forgot to emit proper signals (layoutChanged and dataChanged in proper places)

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

    Default Re: Need help with QTreeView/QAbstractItemModel

    I can't just use QStringListModel, since as I said I'm going to use a database to extract the information from. The QStringList is just a placeholder until I get to grasps with the Model/View structure.
    Where should I emit the dataChanged() signal from?

  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: Need help with QTreeView/QAbstractItemModel

    You should emit dataChanged whenever the data of existing items change and layoutChanged whenever you add or remove items (or reorder them).

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

    Default Re: Need help with QTreeView/QAbstractItemModel

    Bah, I've been trying for a while now, and I can't figure out how to get the index() and parent() functions to work. Basically all I'm doing is reading a qstring returned from a qdailog and adding it to a qstringlist. How do I connect this to the QAbstractItemModel?

  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: Need help with QTreeView/QAbstractItemModel

    IMO your index() should look like so:

    Qt Code:
    1. QModelIndex serverListModel::index( int row, int column, const QModelIndex &parent ) const {
    2. return createIndex(row, column);
    3. }
    To copy to clipboard, switch view to plain text mode 

    For parent and a "flat" (tabular) model you can always return QModelIndex() as all items have an invalid parent.

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.