QT4 QstringListModel in QListView

那年仲夏 提交于 2019-12-04 06:57:05

You've modified the QStringList, you need to modify the model:

stringList->append("xyz");
listModel->setStringList(*stringList);

If you frequently need to modify the string list and have connected views that need to be updated, you could consider doing away with the QStringList in the first place and solely using the QStringListModel. You can add/remove data there using insertRows/removeRows and setData. This ensures the views always reflect the model in the way you would expect. This could be wrapped to prevent tedious work. Something like (untested):

class StringList : public QStringListModel
{
public:
  void append (const QString& string){
    insertRows(rowCount(), 1);
    setData(index(rowCount()-1), string);
  }
  StringList& operator<<(const QString& string){
    append(string);
    return *this;
  }
};
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!