[英]How to update a QStringListModel?
I have the following code:我有以下代码:
QStringListModel* m=new QStringListModel(gc.get_lista_file());
ui->lista_immagini_listView->setModel(m);
where gc.get_lista_file()
returns a QStringList
object and lista_immagini_listView
is a QListView
.其中
gc.get_lista_file()
返回一个QStringList
对象,而lista_immagini_listView
是一个QListView
。
I need to update my lista_immagini_listView
adding a string when I press a button, but if I add my new string to my QStringList
object it doesn't update my model (I read the QStringList
is passed by copy, so it isn't connected to the model).当我按下按钮时,我需要更新我的
lista_immagini_listView
添加一个字符串,但是如果我将我的新字符串添加到我的QStringList
对象中,它不会更新我的模型(我读到QStringList
是通过副本传递的,所以它没有连接到模型)。 So, I have to update my QStringListModel
but in this way I have to update 2 object ( QStringList
and QStringListModel
) and doesn't seem a good practice.所以,我必须更新我的
QStringListModel
但这样我必须更新 2 个对象( QStringList
和QStringListModel
)并且似乎不是一个好习惯。
What is the best way (if exists) to resolve it?解决它的最佳方法(如果存在)是什么?
QStringListModel
does not allow you to simply add a string (sadly). QStringListModel
不允许您简单地添加一个字符串(遗憾的是)。 Simply updating the QStringList
does not work because the model stores a copy of the list.简单地更新
QStringList
是行不通的,因为模型存储了列表的副本。
There are basically two ways to get the desired behavior:基本上有两种方法可以获得所需的行为:
1. Reset: 1. 重置:
This is the simple way.这是简单的方法。 You just take the list from the model, add the string and reassign it:
您只需从模型中获取列表,添加字符串并重新分配它:
QStringList list = m->stringList();
list.append("someString");
m->setStringList(list);
This method does work, but has one big disadvantage: The view will be reset.这种方法确实有效,但有一个很大的缺点:视图将被重置。 Any selections the user may have, sorting or the scroll-position will be lost, because the model gets reset.
用户可能拥有的任何选择、排序或滚动位置都将丢失,因为模型被重置。
2. Using the Model: 2.使用模型:
The second approach is the proper way of doing, but requires some more work.第二种方法是正确的做法,但需要做更多的工作。 In this you use the functions of
QAbstractItemModel
to first add a row, and then changing it's data:在这里,您使用
QAbstractItemModel
的函数首先添加一行,然后更改它的数据:
if(m->insertRow(m->rowCount())) {
QModelIndex index = m->index(m->rowCount() - 1, 0);
m->setData(index, "someString");
}
This one does properly update the view and keeps it's state.这个确实正确地更新了视图并保持它的状态。 However, this one gets more complicated if you want to insert multiple rows, or remove/move them.
但是,如果您想插入多行或删除/移动它们,这会变得更加复杂。
My recommendation: Use the 2. Method, because the user experience is much better.我的建议:使用2.方法,因为用户体验要好得多。 Even if you use the list in multiple places, you can get the list after inserting the row using
m->stringList()
.即使您在多个地方使用列表,您也可以在使用
m->stringList()
插入行后获取列表。
You need to only use the string list provided by the QStringListModel
- don't keep a separate copy, use QStringListModel::stringList()
for reading only .您只需要使用
QStringListModel
提供的字符串列表 - 不要保留单独的副本,使用QStringListModel::stringList()
进行只读。 To modify the list, use the model's methods: insertRows
, removeRows
and setData
instead of using QStringList
methods.要修改列表,请使用模型的方法:
insertRows
、 removeRows
和setData
而不是使用QStringList
方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.