Results 1 to 9 of 9

Thread: Updating QTreeView with custom model

  1. #1
    Join Date
    Aug 2008
    Posts
    35
    Thanks
    7
    Thanked 3 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Updating QTreeView with custom model

    I really need some help updating my QTreeView. I have been looking all over the forum for solutions but I couldn't find any that worked...

    This is the code I got:
    Qt Code:
    1. PlaylistModel* plm = NULL;
    2.  
    3. MainWindow::MainWindow()
    4. {
    5. setupUi(this);
    6. plm = new PlaylistModel(this);
    7. treeView->setModel(plm);
    8. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. extern Playlist* currentPlaylist;
    2.  
    3. QVariant PlaylistModel::data(const QModelIndex &index, int role) const
    4. {
    5. if(!index.isValid())
    6. return QVariant();
    7. if(index.row() >= currentPlaylist->size())
    8. return QVariant();
    9. if(role == Qt::DisplayRole)
    10. {
    11. Song* song = currentPlaylist->at(index.row());
    12. if(song)
    13. {
    14. std::cout << "Song found!" << std::endl;
    15. if(song->info)
    16. {
    17. std::cout << "Info found!" << std::endl;
    18. if(index.column() == 0)
    19. return "Title";
    20. else if(index.column() == 1)
    21. return "Artist";
    22. }
    23. }
    24. return "N/A";
    25. }
    26. else
    27. return QVariant();
    28. }
    29.  
    30. QVariant PlaylistModel::headerData(int section, Qt::Orientation orientation, int role) const
    31. {
    32. if(role != Qt::DisplayRole)
    33. return QVariant();
    34. if(orientation == Qt::Horizontal)
    35. return QString("Column %1").arg(section);
    36. else
    37. return QString("Row %1").arg(section);
    38. }
    39.  
    40. int PlaylistModel::rowCount(const QModelIndex &parent) const
    41. {
    42. return currentPlaylist->size();
    43. }
    44.  
    45. int PlaylistModel::columnCount(const QModelIndex &parent) const
    46. {
    47. return 2;
    48. }
    49.  
    50. void PlaylistModel::update()
    51. {
    52. QModelIndex topLeft = createIndex(0, 0);
    53. QModelIndex bottomRight = createIndex(currentPlaylist->size()-1, 1);
    54. dataChanged(topLeft, bottomRight);
    55. }
    To copy to clipboard, switch view to plain text mode 

    As you can see, the data is stored in currentPlaylist (the Playlist class is just a QList). But when I add data to the currentPlaylist, it doesn't show it on the treeView... But when I add data to the currentPlaylist before I construct the tableView and model, it does show the data!

    So I have tried to make an update function. But that doesn't work either...

    Thanks in advance,
    Gillis

  2. #2
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 162 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Updating QTreeView with custom model

    You are aware that you have to wrap adding rows in calls to
    QAbstractItemModel::beginInsertRows(),
    QAbstractItemModel::endInsertRows()?

    otherwise, your model('s base class) does not know its underlying data got added to and it can not tell the view about it.

  3. #3
    Join Date
    Aug 2008
    Posts
    35
    Thanks
    7
    Thanked 3 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Updating QTreeView with custom model

    What must I do then when importing files?
    I got this now:
    Qt Code:
    1. void GMainWindow::addFiles()
    2. {
    3. QFileDialog dialog(this, "Import Music File", "", "MP3 Files (*.mp3)");
    4. dialog.setFileMode(QFileDialog::ExistingFiles);
    5. if(dialog.exec())
    6. {
    7. QStringList fileNames = dialog.selectedFiles();
    8. for(int i = 0; i < fileNames.size(); ++i)
    9. {
    10. GFileInfo* fileInfo = new GFileInfo(fileNames.at(i));
    11. Song* song = new Song();
    12. song->info = fileInfo;
    13. currentPlaylist->append(song);
    14. }
    15. /*PlaylistModel* model = (PlaylistModel*)treeView->model();
    16. model->update();*/
    17. }
    18. }
    To copy to clipboard, switch view to plain text mode 
    Should I add model->beginInsertRows(); at the beginning of the function and model->endInsertRows(); at the end?

  4. #4
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 162 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Updating QTreeView with custom model

    Add a method append to your model, and call the model's append method!
    (Never modify the underlying data except through the model, otherwise the model won't know about it and views can't get updated.)

    Qt Code:
    1. PlayListModel::append(Song *song)
    2. {
    3. beginInsertRows(QModeIndex(), rowCount(), rowCount());
    4. currentPlaylist->append(song);
    5. endInsertRows();
    6. }
    To copy to clipboard, switch view to plain text mode 

    call this method in your addFiles()

    HTH

  5. #5
    Join Date
    Aug 2008
    Posts
    35
    Thanks
    7
    Thanked 3 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Updating QTreeView with custom model

    That works! But I still find it a bit weird... There should be a function or something to notify the model, that would be alot easier

    Thanks!

  6. #6
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 162 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Updating QTreeView with custom model

    Well, if class X has all the possibilities to inform a model M about the relevant changes, then that class X already is (has to be) a model and M is a mere proxy (model).

    (I have a list class I wrote before Qt introduced the model/view concept that I used as my own model before. I have written an adapter to plug that into a Qt-model. No need to do so when you start from scratch, though.)

  7. #7
    Join Date
    Aug 2008
    Posts
    35
    Thanks
    7
    Thanked 3 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Updating QTreeView with custom model

    And if I want to sort the tree on 'Artist' for example, and I want to do it by sorting the currentPlaylist and then updating the view instead of sorting the model?

  8. #8
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Updating QTreeView with custom model

    You dont have to sort the actual model. You can use a proxy model.
    Have a look at QSortFilterProxyModel . And also the examples in Qt Demo

  9. #9
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 162 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Updating QTreeView with custom model

    I second that you should use QSortFilterProxyModel here. It's easier (otherwhise you have to connect the QHeaderView with some custom slot etc...), with QSortFilterProxyModel all you need is there.

    If you should, however, really sort your model's underlying data, you have to tell the model about it: subclass it, make reset() public and call it.

Similar Threads

  1. Custom proxy model issue
    By Khal Drogo in forum Qt Programming
    Replies: 13
    Last Post: 30th November 2007, 12:41
  2. Custom Model Help PLz
    By Simz in forum Qt Programming
    Replies: 3
    Last Post: 16th August 2007, 15:28
  3. Treeview and custom model
    By steg90 in forum Qt Programming
    Replies: 8
    Last Post: 15th May 2007, 13:54
  4. Modify model data in QTreeView
    By YuriyRusinov in forum Qt Programming
    Replies: 6
    Last Post: 26th October 2006, 17:28
  5. Add custom QTreeView in VS .NET
    By wind in forum Newbie
    Replies: 4
    Last Post: 5th October 2006, 17:04

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.