如何在数据更改时显示QAbstractListModel的单个项目并更新Gui

时间:2019-04-15 18:18:03

标签: qt qml qabstractlistmodel

我想从QAbstractItemModel中显示单个项目及其在GUI中的角色。在手册中,我只能找到如何显示整个模型。

显示单个项目是可行的,但是当更改模型中的数据时,GUI中的数据不会更新。

模型在setData()函数中发出dataChanged()。

模型中的对象

#ifndef DATASOURCEOBJECT_H
#define DATASOURCEOBJECT_H

#include <QString>
#include <QVariantMap>

class DataSourceObject
{
public:
    DataSourceObject(const int &id=0, const QString &name="", const QString &displayname="", const double &value=0.0);
    DataSourceObject(const QJsonObject &obj);
    int id() const;
    void setId(int id);

    QString name() const;
    void setName(const QString &name);

    QString unit() const;
    void setUnit(const QString &unit);

    double value() const;
    void setValue(double value);
    QVariantMap toMap() const;

private:
    int m_id;
    QString m_name;
    QString m_unit;
    double m_value;
};

#endif // DATASOURCEOBJECT_H

模型标题:

#ifndef DATASOURCEMODEL_H
#define DATASOURCEMODEL_H

#include "datasourceobject.h"
#include <QAbstractListModel>

class DataSourceModel : public QAbstractListModel
{
    Q_OBJECT

public:
    enum datasourceRoles {
        idRole = Qt::UserRole ,
        nameRole,
        unitRole,
        valueRole
    };

    explicit DataSourceModel(QObject *parent = nullptr);
    void addDataSourceObject(const DataSourceObject &dataSourceObject);
    Q_INVOKABLE QVariantMap get(int row) const;

    int rowCount(const QModelIndex &parent = QModelIndex()) const override;
    Q_INVOKABLE QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
    bool setData(const QModelIndex &index, const QVariant &value,
                 int role = Qt::EditRole) override;
    Qt::ItemFlags flags(const QModelIndex& index) const override;
    QHash<int, QByteArray> roleNames() const override;
    //bool checkIndex(const QModelIndex &index) const;

private:
    QList<DataSourceObject> m_DataSourceObjects;
};

#endif // DATASOURCEMODEL_H

模型cpp:

#include "datasourcemodel.h"

DataSourceModel::DataSourceModel(QObject *parent)
    : QAbstractListModel(parent)
{
}

QVariantMap DataSourceModel::get(int row) const
{
    return m_DataSourceObjects[row].toMap();
}

void DataSourceModel::addDataSourceObject(const DataSourceObject &dataSourceObject)
{
    beginInsertRows(QModelIndex(), rowCount(), rowCount());
    m_DataSourceObjects << dataSourceObject;
    endInsertRows();
}
int DataSourceModel::rowCount(const QModelIndex &parent) const
{
    if (parent.isValid())
        return 0;
    return m_DataSourceObjects.count();
}

QVariant DataSourceModel::data(const QModelIndex &index, int role) const
{
    if(index.row() < 0 || index.row() >= m_DataSourceObjects.count() || !index.isValid())
        return  QVariant();

    const DataSourceObject &dataSourceObject = m_DataSourceObjects[index.row()];
    if (role == idRole)
        return dataSourceObject.id();
    else if (role == nameRole)
        return dataSourceObject.name();
    else if (role == unitRole) {
        return dataSourceObject.unit();
    }
    else if (role == valueRole)
        return dataSourceObject.value();
    return QVariant();
}

bool DataSourceModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
    DataSourceObject &dataSourceObject = m_DataSourceObjects[index.row()];
    if (data(index, role) != value) {
        if(role == idRole)
            dataSourceObject.setId(value.toInt());
        else if(role == nameRole)
            dataSourceObject.setName(value.toString());
        else if(role == unitRole)
            dataSourceObject.setUnit(value.toString());
        else if(role == valueRole)
            dataSourceObject.setValue(value.toDouble());
        emit dataChanged(index, index, QVector<int>() << role);
        return true;
    }
    return false;
}

Qt::ItemFlags DataSourceModel::flags(const QModelIndex &index) const
{
    if (!index.isValid())
        return Qt::NoItemFlags;

    return Qt::ItemIsEditable; // FIXME: Implement me!
}

QHash<int, QByteArray> DataSourceModel::roleNames() const
{
    QHash<int, QByteArray> roles;
    roles[idRole] = "id";
    roles[nameRole] = "name";
    roles[unitRole] = "unit";
    roles[valueRole] = "value";
    return  roles;
}

我试图像这样访问QML中的单个项目:

 Text {
       text: dataSourceModel.data(dataSourceModel.index(88,0),259).toFixed(decimalplaces)
      }

这显示数据的角色259(valueRole),但更改后文本不会更新。

我试图从QObject派生模型内部的对象,以使用Q_INVOKABLE,但是从QOBject派生时,我无法编译它。C:\ Qt \ 5.10.0 \ mingw53_32 \ include \ QtCore \ qlist.h :435:Fehler:使用已删除的函数'DataSourceObject :: DataSourceObject(const DataSourceObject&)'      if(QTypeInfo :: isLarge || QTypeInfo :: isStatic)n-> v = new T(t);

                                                           ^

1 个答案:

答案 0 :(得分:1)

该文本不会更新,因为您仅复制了该值,但各组件之间没有绑定。

一种可行的解决方案是,如果行是指定的行,则在创建项目的加载程序旁边使用中继器。

Component {
    id: textcomponent
    Text {
        text: value.toFixed(decimalplaces)
    }
}
Repeater{
    model: dataSourceModel
    Loader{
        property real value: model.value
        sourceComponent: index == 88 ? textcomponent: null
    }
}

完整的示例,您可以找到https://docs.projectcalico.org/v3.6/getting-started/kubernetes/installation/calico

另一种可能的解决方案是将第88项专门设置为Q_PROPERTY。