QStringListModel doesn't update ListView



  • Hi,

    I have a QStringListModel where I set the StringList everytime I've added a new item to my Stringlist with the following code:

    @
    model->setStringList(stringList);
    @

    As the documentation tells me this method will notify each view about changes.
    rowCount tells me everytime the correct value of items containing, but my list view doesn't get updated (max. displayed items are 1 or 0).
    I let qml know about the changes with the following code in my main:

    @
    view.rootContext()->setContextProperty("myVar", QVariant::fromValue(list));
    @
    Did I miss something or what could be the problem I'm facing?

    So long.



  • Hello,

    you had to execute the last line of code each time you chang the model.



  • Ok I now get the view updated. I had somewhere a

    @QStringList list = myList.stringList()@

    and this list got added to the ListView in QML, so no updates possible. Now I changed the context property to the correct list, but I don't know how to get the QString of the QStringListModel inside of qml? Someone an idea?



  • You can check the precedent post : the solution is in it : myVar.modelData in the delagate.
    Or with myVar[index] in javascript



  • Both myVar.modelData as well as myVar[index] tell me that it's undefined.

    I changed the context property in main to the following:
    @view.rootContext()->setContextProperty("myVar", myList)@

    Now he shows me each rectangle of the list i should have, but I don't know how to get the strings out of the model.



  • Could you bring a bench of code, it help us to have an idea of what's going wrong



  • I inherited for QStringListModel so that I could access it from QML. With this you can modelData inside QML.

    @class NIQStringListModel : public QStringListModel
    {
    Q_OBJECT
    public:
    explicit NIQStringListModel (QObject *parent = 0);

    void         append             (const QString& string);
    

    public slots:
    int length () const { return rowCount(); }
    QStringList getStringList () const { return stringList(); }
    QString dataAt (int i) const;
    };@

    @NIQStringListModel::NIQStringListModel(QObject *parent) :
    QStringListModel(parent)
    {
    QHash<int, QByteArray> roles;
    roles[Qt::DisplayRole] = "modelData";
    setRoleNames(roles);
    }

    void NIQStringListModel::append(const QString &string)
    {
    insertRow( rowCount() );
    setData( index( rowCount() - 1 ), string );
    }

    QString NIQStringListModel::dataAt(int i) const
    {
    if ( i >= 0 && i < rowCount() )
    return stringList().at( i );

    return QString();
    

    }@



  • For a simple stringList you don't need to inherit the model, you can just use QVariant::fromValue(yourList).

    Recently i had a same problem, and i didn't use a model but a simple QList<QObject*>, and it works (however you had to call
    @view.rootContext()->setContextProperty("myVar", QVariant::fromValue(list));@ at every change)



  • Ok the version of Zamx is working. But only if I add the items in the same thread the GUI lives in.
    When the model will be filled with items in another thread, then my program will crash because the parent of QModelIndex will be null which seems to be quite strange.
    Any idea how I can get the model work in another thread?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.