Quantcast
Channel: Active questions tagged qtquick2 - Stack Overflow
Viewing all articles
Browse latest Browse all 107

Why is one QML ListModel in my application not beeing replaced?

$
0
0

I am trying to set up a data-based qml element list. As a first step I just want to update the list by data, not by manual input. In the constructor of the list I append 2 list elements. After that, I want to update the list by data from an external data source. The data source is simply a self-created list of strings and inserted into the model via UpdateElements(QList<QString> newList). With the here shown code I can update my list, but as you can see on the attached screenshot, the first initially (in the constructor) created list element "Wash the car" is still beeing displayed . What am I doing wrong here? The other initial list element "Fix the sink" is beeing replaced.

And here an addon question: Is this model way really the best practice for dynamic lists in qt? From my point of view it is very intransparent when what is called and what it is actually doing, thats why I am mainly struggeling where the problem lies.

enter image description here

Here you can see my source code which was based on a tutorial on youtube, since I am really new in qt. I am sorry but I dont know how I could strip this code example down further.

#include "namelist.h"NameList::NameList(QObject *parent)    : QObject{parent}{    m_Items.append({QStringLiteral("Wash the car"),  0});    m_Items.append({QStringLiteral("Fix the sink"),  1});}QVector<NameListItem> NameList::items() const{    return m_Items;}void NameList::UpdateElements(QList<QString> newList){    m_Items.clear();    int id = 0;    for(QString i : newList)    {        appendItem(i, id);        id++;    };}void NameList::appendItem(QString name, int id){    emit preItemAppend();    NameListItem item;    item.name= name;    item.id = id;    m_Items.append(item)    emit postItemAppend();}void NameList::removeItem(int id){    m_Items.remove(id);}
#include "namelistmodel.h"#include "namelist.h"NameListModel::NameListModel(QObject *parent): QAbstractListModel(parent), m_List(nullptr){}int NameListModel::rowCount(const QModelIndex &parent) const{    if (parent.isValid())        return 0;    else        return m_List->items().size();}QVariant NameListModel::data(const QModelIndex &index, int role) const{    if (!index.isValid())        return QVariant();    const NameListItem item = m_List->items().at(index.row());    switch(role){    case NameRole:        return QVariant(item.name);    case IdRole:        return QVariant(item.id);    }    return QVariant();}Qt::ItemFlags NameListModel::flags(const QModelIndex &index) const{    if (!index.isValid())        return Qt::NoItemFlags;    return Qt::ItemIsEditable; // FIXME: Implement me!}QHash<int, QByteArray> NameListModel::roleNames() const{    QHash<int, QByteArray> names;    names[IdRole] = "id";    names[NameRole] = "name";    return names;}NameList *NameListModel::list() const{    return m_List;}void NameListModel::appendItemQML(){    const int index = m_List->items().size();    beginInsertRows(QModelIndex(), index, index);}void NameListModel::finishAppendItemQML(){    endInsertRows();}void NameListModel::removeItemQML(int index){    beginRemoveRows(QModelIndex(), index, index);}void NameListModel::finishRemoveItemQML(){    endRemoveRows();}void NameListModel::setList(NameList *list){    beginResetModel();    if(m_List){        m_List->disconnect(this);    }    m_List = list;    if(m_List){        connect(m_List, &NameList::preItemAppend, this, &NameListModel::appendItemQML);        connect(m_List, &NameList::postItemAppend, this, &NameListModel::endInsertRows);        connect(m_List, &NameList::preItemRemoved, this, &NameListModel::removeItemQML);        connect(m_List, &NameList::postItemRemoved, this, &NameListModel::endRemoveRows);    }    endResetModel();}

Viewing all articles
Browse latest Browse all 107

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>