Results 1 to 3 of 3

Thread: Moving items between models

  1. #1
    Join Date
    Aug 2010
    Posts
    99
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Moving items between models

    I am trying to make a dialog with two lists, where items can be moved between each list, like so:



    I am using a QListView and QStringListModel. How can i move the selected items from one list model to the other?

    This is what i have got so far:
    Qt Code:
    1. void moveToRight() {
    2. QStringListModel* leftModel = (QStringListModel*)leftListView->model();
    3. QStringListModel* rightModel = (QStringListModel*)rightListView->model();
    4.  
    5. leftListView->setUpdatesEnabled(false);
    6. rightListView->setUpdatesEnabled(false);
    7.  
    8. QModelIndexList indexes = leftListView->selectionModel()->selectedIndexes();
    9. qSort(indexes.begin(), indexes.end());
    10.  
    11. // Add the selected items to the right list
    12. for (int i = 0; i < indexes.count(); i++) {
    13. //rightModel->insertRow(indexes.at(i).row());
    14. // I'm guessing i could get the QStringList from the model
    15. // and add to that.
    16. }
    17.  
    18. // Then remove them from the left list
    19. for (int i = indexes.count() - 1; i > -1; --i) {
    20. leftModel->removeRow(0);
    21. }
    22.  
    23. leftListView->setUpdatesEnabled(true);
    24. rightListView->setUpdatesEnabled(true);
    25. }
    To copy to clipboard, switch view to plain text mode 

    I do not yet know of a way to add the items to the other model. Also, the removing doesn't seem to work.
    [Window Detective] - Windows UI spy utility
    [War Thunder]

  2. #2
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Moving items between models

    This code works fine

    Qt Code:
    1. void Widget::on_moveRightButton_clicked()
    2. {
    3. QModelIndexList leftSelection = ui->leftView->selectionModel ()->selectedIndexes ();
    4.  
    5. int rowCount;
    6. Q_FOREACH (QModelIndex idx, leftSelection) {
    7. rowCount = m_rightModel.rowCount ();
    8. m_rightModel.insertRow (rowCount);
    9.  
    10. m_rightModel.setData (m_rightModel.index (rowCount), idx.data ());
    11. }
    12.  
    13. for (int i = leftSelection.size (); i > 0; --i) {
    14. m_leftModel.removeRow (leftSelection.at (i - 1).row ());
    15. }
    16. }
    17.  
    18. void Widget::on_moveLeftButton_clicked()
    19. {
    20. QModelIndexList rightSelection = ui->rightView->selectionModel ()->selectedIndexes ();
    21.  
    22. int rowCount;
    23. Q_FOREACH (QModelIndex idx, rightSelection) {
    24. rowCount = m_leftModel.rowCount ();
    25. m_leftModel.insertRow (rowCount);
    26.  
    27. m_leftModel.setData (m_leftModel.index (rowCount), idx.data ());
    28. }
    29.  
    30. for (int i = rightSelection.size (); i > 0; --i) {
    31. m_rightModel.removeRow (rightSelection.at (i - 1).row ());
    32. }
    33. }
    To copy to clipboard, switch view to plain text mode 
    A camel can go 14 days without drink,
    I can't!!!

  3. #3
    Join Date
    Aug 2010
    Posts
    99
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Moving items between models

    Yeah, that worked. Thanks.

    I suppose i could have figured it out myself, i just though i'd ask in-case there was a better way. Btw, in your example, you didn't sort the indexes. I think this is necessary in order to remove them from the model without invalidating the indexes (at least, that's what i heard).

Similar Threads

  1. Moving items in QAbstractItemModel
    By arturo182 in forum Qt Programming
    Replies: 13
    Last Post: 1st June 2011, 00:53
  2. Moving Items with itemChange?
    By konvex in forum Qt Programming
    Replies: 5
    Last Post: 21st November 2008, 14:36
  3. Moving items within a model
    By iswm in forum Qt Programming
    Replies: 1
    Last Post: 11th April 2007, 08:29
  4. Moving items between two views.
    By YuriyRusinov in forum Qt Programming
    Replies: 11
    Last Post: 2nd April 2007, 13:53
  5. moving Qlistview items
    By :db:sStrong in forum Qt Programming
    Replies: 0
    Last Post: 21st February 2006, 12:25

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.