Appending Items to QListView
-
I am currently trying to append items to a list view. I found a code snippet online that appends a single Item to a list view by using
@
QStandardItem* Items = new QStandardItem(myString);
QStandardItemModel* ListModel = new QStandardItemModel();
ListModel->appendRow(Items);ui->lvconnect->setModel(ListModel);
@
The thing is, every time through, myString is going to change, and I just want to add the new Item to the list without removing any items from the list view. I figured appendRow would do this, but it seems it wont. I have it connected to a button, so everytime I push the button, myString should be added to the list view. There isn't much documentation on how to add items to the list view and this is the only helpful code snippet I could find. I have read the listView documentation as well.
Any help would be great.
EDIT: It seems I am over writing the model every time. That's why I am having issue with displaying a different item every time. I still havent found a solution yet though...I am trying to make things global currently.
-
QListView is only a representation for your data stored in model. So question itself is a bit wrong. You shouldn't think that you add items to view, you add them to model and view only shows items that are stored in model.
And of course you don't need to create model each time you add item. All you need is to get model() from view (or store it somewhere else, it depends on your arch) and add row to it.
-
-
The best way to do it is to subclass QAbstractItemModel and do checks on items adding.
-
or use "QStandardItemModel::findItems() ":http://doc.qt.nokia.com/4.7/qstandarditemmodel.html#findItems method to check if it's already in the model.