PDA

View Full Version : How to delete item from QListView?



damon_1990
4th October 2011, 20:10
Hi,

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


QStringList list;
list << "1" << "2" << "3" << "4" << "5";

QStringListModel model = new QStringListModel;
model->setStringList(list);

QListView *listView = new QListView;
listView->setModel(model);

So, what is the simpliest way to do this? Thanks in advance.

Lykurg
4th October 2011, 21:29
First you have alter the model: e.g. QAbstractItemModel::removeRow(). To get the current selected item: QAbstractItemView::selectionModel() and then QItemSelectionModel::currentIndex().

damon_1990
4th October 2011, 22:29
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?

Lykurg
4th October 2011, 22:33
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!

damon_1990
4th October 2011, 22:47
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.