Results 1 to 5 of 5

Thread: RemoveRow problem

  1. #1
    Join Date
    Aug 2007
    Posts
    19
    Thanks
    5
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default RemoveRow problem

    Im having some trouble with the QAbstractItemModel's remove row function working with a QTableView re-implementation. I call the function with the index from a contextMenuEvent and delete it accordingly:
    Qt Code:
    1. void WTrackTableView :: contextMenuEvent(QContextMenuEvent * event)
    2. {
    3. index = indexAt(event->pos());
    4. m_pTrackInfoObject = m_pTable->m_pTrackCollection->getTrack(index.row()+1);
    5. if(index.isValid())
    6. {
    7. QMenu menu(this);
    8. menu.addAction(PlayQueueAct);
    9. menu.addAction(Player1Act);
    10. menu.addAction(Player2Act);
    11. menu.addAction(RemoveAct);
    12. menu.exec(event->globalPos());
    13.  
    14. }
    15. }
    16.  
    17. void WTrackTableView :: slotRemoveFromPlaylist()
    18. {
    19. qDebug("%i",index.row());
    20. m_pTable->removeRow(index.row(),index);
    21. }
    To copy to clipboard, switch view to plain text mode 
    index is a QModelIndex created in the header file so I can use it across functions. now, my problem is that if I select an item anywhere in the view the item still stays in the table but the item at the end of the table is removed. why would this be happening? The qDebug that I have returns the correct reference to the row that is selected, but nothing happens at that index.

  2. #2
    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: RemoveRow problem

    May we see the removeRows() implementation of the model? Notice that the second parameter of QAbstractItemModel::removeRow() is parent of item being removed so for a flat model it should be an invalid index.
    J-P Nurmi

  3. #3
    Join Date
    Aug 2007
    Posts
    19
    Thanks
    5
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: RemoveRow problem

    there is no removeRows implementation, that is using the the
    Qt Code:
    1. bool QAbstractItemModel::removeRow ( int row, const QModelIndex & parent = QModelIndex() )
    To copy to clipboard, switch view to plain text mode 
    call.

  4. #4
    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: RemoveRow problem

    QAbstractItemModel::removeRow() is a convenience function that further calls QAbstractItemModel::removeRows(). QAbstractItemModel::removeRows(), on the other hand, does nothing but returns false. You should provide a proper re-implementation for QAbstractItemModel::removeRows(). This includes calling QAbstractItemModel::beginRemoveRows(), modifying the internal data structure, and then calling QAbstractItemModel::endRemoveRows(). This ensures that the view gets updated correctly.
    J-P Nurmi

  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: RemoveRow problem

    This is for example how QStringListModel implements removeRows():
    Qt Code:
    1. bool QStringListModel::removeRows(int row, int count, const QModelIndex &parent)
    2. {
    3. Q_UNUSED(parent);
    4. if (count <= 0 || row < 0 || (row + count) > rowCount(parent))
    5. return false;
    6.  
    7. beginRemoveRows(QModelIndex(), row, row + count - 1);
    8.  
    9. for (int r = 0; r < count; ++r)
    10. lst.removeAt(row);
    11.  
    12. endRemoveRows();
    13.  
    14. return true;
    15. }
    To copy to clipboard, switch view to plain text mode 
    Here "lst" is the internal data structure, a QStringList.
    J-P Nurmi

Similar Threads

  1. Graphics view display problem.
    By kiranraj in forum Qt Programming
    Replies: 3
    Last Post: 20th July 2007, 07:08
  2. [QMYSQL] connection problem
    By chaos_theory in forum Installation and Deployment
    Replies: 5
    Last Post: 2nd July 2007, 09:52
  3. QTimer problem ... it runs but never triggs
    By yellowmat in forum Newbie
    Replies: 4
    Last Post: 4th July 2006, 12:54
  4. Grid Layout Problem
    By Seema Rao in forum Qt Programming
    Replies: 2
    Last Post: 4th May 2006, 12:45
  5. Replies: 16
    Last Post: 7th March 2006, 15:57

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.