Results 1 to 5 of 5

Thread: How to delete item from QListView?

  1. #1
    Join Date
    Sep 2011
    Posts
    10
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default How to delete item from QListView?

    Hi,

    I would like to delete selected item from QListView, but I don't know how to get current index. I have code:

    Qt Code:
    1. list << "1" << "2" << "3" << "4" << "5";
    2.  
    3. model->setStringList(list);
    4.  
    5. QListView *listView = new QListView;
    6. listView->setModel(model);
    To copy to clipboard, switch view to plain text mode 

    So, what is the simpliest way to do this? Thanks in advance.
    Last edited by damon_1990; 4th October 2011 at 21:22.

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: How to delete item from QListView?

    First you have alter the model: e.g. QAbstractItemModel::removeRow(). To get the current selected item: QAbstractItemView::selectionModel() and then QItemSelectionModel::currentIndex().

  3. #3
    Join Date
    Sep 2011
    Posts
    10
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to delete item from QListView?

    Thanks for answer. I'll try to change my code but now I'm not sure I chose the corret class. I'm using QListView to display data from SQL table. I'm reading records and putting them in QStringList. Next I'm creating my model. User can add/delete new record to QListView/database. So in that case, should I use QListWidget or QListView? What do you think would be better for me to use for this?

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: How to delete item from QListView?

    Ehm, QTreeView with a QSqlTableModel or QSqlQueryModel. That's the standard case for them. Seriously, spend some time to read the parts about model and views at the docs. It might be difficult in the first place BUT it will save you a lot of pain in the a***!

    Use QTreeView!

  5. The following user says thank you to Lykurg for this useful post:

    damon_1990 (4th October 2011)

  6. #5
    Join Date
    Sep 2011
    Posts
    10
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to delete item from QListView?

    Ok thanks for help I chose QListView because I want to display only one column from table, but maybe it was more easy to use QSqlTableModel and hide columns.

Similar Threads

  1. Replies: 1
    Last Post: 4th August 2011, 23:36
  2. Replies: 3
    Last Post: 21st July 2010, 08:40
  3. Delete item from QMap<int, QString> with shift
    By Usernаme in forum Newbie
    Replies: 1
    Last Post: 24th May 2010, 19:50
  4. How to use QMap::remove() to delete some item?
    By jedychen in forum Qt Programming
    Replies: 5
    Last Post: 18th September 2008, 08:29
  5. Replies: 5
    Last Post: 1st March 2007, 09:19

Tags for this Thread

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.