Results 1 to 5 of 5

Thread: Remove item from QListView's model

  1. #1
    Join Date
    Jun 2013
    Posts
    22
    Thanks
    1
    Qt products
    Qt5

    Default Remove item from QListView's model

    Hello!

    I'm trying to remove one of few listed files from model of QListView. I'm using this code but it doesn't work:
    Qt Code:
    1. ui->listView->model()->removeRow(ui->viewFiles->currentIndex().row());
    To copy to clipboard, switch view to plain text mode 
    Same thing is if I use model->removeRow(..same as above..);

    What's wrong with this code and how to delete item from list?

    Thank you!

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Remove item from QListView's model

    If this is a QFileSystemModel then it has a remove method that takes a model index and removes the file represented by that model index.
    Seems removeRow() does not change the files on disk, thus not changing the actual data the model works on.

    Cheers,
    _

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Remove item from QListView's model

    How are the models underlying ui->listView and ui->viewFiles related? Are they pointing at the same model? Are there proxy models involved? What type is the underlying model?

  4. #4
    Join Date
    Jun 2013
    Posts
    22
    Thanks
    1
    Qt products
    Qt5

    Default Re: Remove item from QListView's model

    @anda_skoa: I don't want to write changes (to delete file) on disk but only in application's list.
    @ChrisW67:
    ui->listView = ui->viewFiles (I was just testing out few commands which I found on StackOverflow and forget to change viewFiles to listView)
    What are proxy models?
    If underlying model means model which is set to listView than it's type is QFileSystemModel. As I said, I just want to remove item from list not from disk.

  5. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Remove item from QListView's model

    QFileSystemModel does not implement removeRows() (which removeRow() uses). It does have remove() but that actually removes the file or directory from the file system. One option is to hide the relevant rows in the view with QListView::setRowHidden(). Another option is to use a QSortFilterProxyModel between the QFileSystemModel and the view and filter out the rows that are not to be shown.

Similar Threads

  1. Replies: 9
    Last Post: 14th February 2013, 19:39
  2. Replies: 2
    Last Post: 12th December 2012, 02:40
  3. Replies: 14
    Last Post: 2nd October 2012, 03:10
  4. Using model indices in complex model item relationships
    By hackerNovitiate in forum Newbie
    Replies: 0
    Last Post: 29th June 2011, 14:30
  5. Remove a child in QLIstView (Qt3.3.4)
    By suresh in forum Newbie
    Replies: 2
    Last Post: 7th October 2006, 20:45

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
  •  
Qt is a trademark of The Qt Company.