Search code examples
c++qtqt4qlistview

QT4 QstringListModel in QListView


This is my first QT question - I'm generally a C# programmer so forgive me for asking a stupid question for which I'm sure there's a very simple answer, which I just can't seem to find:

I want to add items to a list, for the moment let's say they're strings. I have a QListView: UI->listView, a QStringList, and a QStringListModel:

stringList = new QStringList();
stringList->append("ABC");
stringList->append("123");

listModel = new QStringListModel(*stringList, NULL);
ui->listView->setModel(listModel);

stringList->append("xyz");

This example compiles and disaplys "ABC" and "123" in my list, but not "xyz". Why not? Do I need to repaint the listView somehow? Have I done something wrong with the NULL?

Thanks.


Solution

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

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