Results 1 to 9 of 9

Thread: Treeview and custom model

  1. #1
    Join Date
    May 2007
    Posts
    301
    Thanks
    46
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Treeview and custom model

    Hi,

    I've started a custom model for a treeview, the code for the model is as follows:

    Qt Code:
    1. #include "dacantreemodel.h"
    2.  
    3. DACanTreeModel::DACanTreeModel(QObject *parent)
    4. {
    5. }
    6.  
    7. DACanTreeModel::~DACanTreeModel()
    8. {
    9. }
    10.  
    11. QModelIndex DACanTreeModel::index( int row, int column, const QModelIndex& parent) const
    12. {
    13. return QModelIndex();
    14. }
    15.  
    16.  
    17. int DACanTreeModel::rowCount(const QModelIndex &parent) const
    18. {
    19. return m_oTreeItems.count();
    20. }
    21.  
    22. int DACanTreeModel::columnCount(const QModelIndex &parent) const
    23. {
    24. return 4;
    25. }
    26.  
    27. QVariant DACanTreeModel::data(const QModelIndex &index, int role) const
    28. {
    29. QVariant data;
    30.  
    31. if (!index.isValid())
    32. return QVariant();
    33. if( role != Qt::DisplayRole )
    34. return QVariant();
    35. if(index.column() > 4 )
    36. {
    37. return QVariant();
    38. }
    39. if( role == Qt::DisplayRole )
    40. data = "Test";
    41.  
    42. return data;
    43. }
    44.  
    45. QVariant DACanTreeModel::headerData(int section, Qt::Orientation orientation,
    46. int role) const
    47. {
    48. if (role != Qt::DisplayRole)
    49. return QVariant();
    50.  
    51. if (orientation == Qt::Horizontal)
    52. return m_Headers[section];
    53. else
    54. return QVariant();
    55. }
    56.  
    57.  
    58. QModelIndex DACanTreeModel::parent(const QModelIndex &index) const
    59. {
    60. return QModelIndex();
    61. }
    62.  
    63. void DACanTreeModel::setHeaders( QStringList& headers )
    64. {
    65. m_Headers = headers;
    66. }
    67.  
    68. void DACanTreeModel::addData( QString strId )
    69. {
    70. CMessage* pMess = new CMessage();
    71. m_oTreeItems.insert( strId, (QObject*)pMess );
    72. }
    To copy to clipboard, switch view to plain text mode 

    I have a QTreeView which sets the model to the above. When I call addData I want an item to be added to the tree but nothing is happening, I guess I'm missing something here?
    In the data function I'm just setting it to 'Test' just to see if this would appear. I have not implemented the index() or the parent() method yet. How do I get something to be put into the tree?

    Regards,
    Steve

  2. #2
    Join Date
    Oct 2006
    Posts
    279
    Thanks
    6
    Thanked 40 Times in 39 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Treeview and custom model

    Qt Code:
    1. void DACanTreeModel::addData( QString strId )
    2. {
    3. beginInsertRows(QModelIndex(), rowCount(), rowCount ());
    4. CMessage* pMess = new CMessage();
    5. m_oTreeItems.insert( strId, (QObject*)pMess );
    6. endInsertRows();
    7. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    May 2007
    Posts
    301
    Thanks
    46
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Treeview and custom model

    Thanks for that Spud,

    To no avail though, nothing is shown, in fact, setting a breakpoint on the data method doesn't break?

    I'm clueless?!?

    Regards,
    Steve

  4. #4
    Join Date
    May 2007
    Posts
    301
    Thanks
    46
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Treeview and custom model

    I've finally got it to break in on the data method by modifying the index method.

    I now have :

    Qt Code:
    1. return createIndex( row, column );
    To copy to clipboard, switch view to plain text mode 

    Not really sure what this does, the modelIndex confuses me?!?

    Regards,
    Steve

  5. #5
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Treeview and custom model

    Remove DACanTreeModel::index(). It's incorrect and makes the model to not work properly. There should be no need to reimplement it.

    Edit: Sorry, forget what I just said. For some reason I read it as QAbstractTableModel. Some other thread must have confused me. For QAbstractItemModel you naturally need to implement index() since it's pure virtual. But do you really need a QAbstractItemModel? In some other thread your data looked flat to me, so maybe you should consider using QAbstractTableModel which makes things easier for you.
    Last edited by jpn; 15th May 2007 at 13:24.
    J-P Nurmi

  6. #6
    Join Date
    Oct 2006
    Posts
    279
    Thanks
    6
    Thanked 40 Times in 39 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Treeview and custom model

    At the moment your model is flat and you can access any item with (row, column), thus your createIndex function can be very simple. If you're intending to make a truly hierarchical model, that won't be enough, though and you will have to store more information in the QModelIndex::internalPointer(). For example you could store a pointer to the parent item.
    If you really need a hierarchical model I suggest you take look at the Simple Tree Model Example. Otherwise it will be a lot easier to go with the QAbstractTableModel as jpn suggested.
    Last edited by spud; 15th May 2007 at 13:38. Reason: spelling

  7. #7
    Join Date
    May 2007
    Posts
    301
    Thanks
    46
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Treeview and custom model

    Hi JPN,

    Don't think the data is flat, well, not 2d, as the first column will show message nodes with their corresponding signal nodes. Unless I'm mistaken, QAbstractTableModel doesn't allow this?

    Please see attachment of what I'm trying to do.

    Regards,
    Steve
    Attached Files Attached Files

  8. #8
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Treeview and custom model

    Quote Originally Posted by steg90 View Post
    Don't think the data is flat, well, not 2d, as the first column will show message nodes with their corresponding signal nodes. Unless I'm mistaken, QAbstractTableModel doesn't allow this?

    Please see attachment of what I'm trying to do.
    Yes, you're right. The model needs to handle parent-child relationships. Well, good luck with implementing a tree model then! Model Test might be handy for checking out the most common mistakes.
    J-P Nurmi

  9. #9
    Join Date
    May 2007
    Posts
    301
    Thanks
    46
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Treeview and custom model

    Thanks JPN,

    I've decided to go for the table model as I don't really need to display the message name and data as the messages are chosen from a previous dialog, this will be the easier option for me as I'm totally confused about the modelIndex and the internalPointer() function?!

    Kind regards,
    Steve

Similar Threads

  1. TreeView custom model
    By steg90 in forum Newbie
    Replies: 1
    Last Post: 9th May 2007, 10:06

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.