I have a QListView _listView
whose model is QStringListModel _model
, whose QStringList
is _locations
. Here's the code:
_locations << "Sarajevo" << "Tesanj" << "Graz";
_model = new QStringListModel(this);
_model->setStringList(_locations);
_listView = new QListView(this);
_listView->setModel(_model);
_listView->setEditTriggers(
QAbstractItemView::EditTrigger::DoubleClicked |
QAbstractItemView::EditTrigger::AnyKeyPressed);
and the slots that edit the _listView
:
void Dialog_EditLocations::onKey_del()
{
QModelIndex _index;
_index = _listView->currentIndex();
_model->removeRow(_index.row());
}
void Dialog_EditLocations::onClick_add()
{
if (_edAddLocation->text() == "") return;
int row = _model->rowCount();
_model->insertRow(row);
QModelIndex _index;
_index = _model->index(row);
_model->setData(_index, _edAddLocation->text());
_edAddLocation->clear();
}
After editing the list in list view widget, I want to store it in a file. When I store the _locations it saves the original list from the first row of the code, even though I added new items.
How to make the code update the _locations
whenever I make a new entry in the _listView
, or at least how to fetch the list that is visible in the _listView
?
Use QStringListModel::stringList()
, it returns all current items from model that is what you see in listview widget, including any modifications (adding,removing rows).
At the end of onClick_add
slot you can add:
_locations = _model->stringList();