qml listview在datachanged()信号之后不更新信息

时间:2016-11-23 05:53:24

标签: c++ qt listview qml qabstractlistmodel

我有一个基于此example编写的模型。此模型连接到listview以显示动态数据。我确保每当更改源数据时都会发出模型的datachanged()信号。 列表视图不会更新datachanged()信号。这是我的代码:

DataModel.h:

class DataList {
    public:
        DataList(const QString &name, const QString &value, const QString &category);

        QString name() const;
        QString value() const;
        QString category() const;

    private:
        QString m_name;
        QString m_value;
        QString m_category;
};

class DataModel : public QAbstractListModel {
    Q_OBJECT
    public:
        enum AVRoles {
            NameRole = Qt::UserRole + 1,
            ValueRole,
            CategoryRole
        };

        DataModel(QAbstractItemModel *parent = 0);
        virtual ~DataModel() {}
        void addData(const DataList &datalist);
        int rowCount(const QModelIndex & parent = QModelIndex()) const;

        QVariant data(const QModelIndex & index, int role=Qt::DisplayRole) const;
    protected:
        QHash<int, QByteArray> roleNames() const;

    private:
        QList<DataList> m_datalist;

    public slots:
        void onInitialPolygonAreaChanged(qreal);

};

DataModel.cpp:

DataList::DataList(const QString &name, const QString &value, const QString &category) {
    m_name = name;
    m_value = value;
    m_category = category;
}

QString DataList::name() const {
    return m_name;    
}

QString DataList::value() const {
    return m_value;
}

QString DataList::category() const {
    return m_category;
}


DataModel::DataModel(QAbstractItemModel *parent) :
    QAbstractListModel(parent) {    
}

void DataModel::addData(const DataList &datalist){
    beginInsertRows(QModelIndex(), rowCount(), rowCount());
    m_datalist << datalist;
    endInsertRows();    
}

int DataModel::rowCount(const QModelIndex &parent) const{
    Q_UNUSED(parent);
    return m_datalist.count();
}

QVariant DataModel::data(const QModelIndex &index, int role) const {
    if (index.row() < 0 || index.row() >= m_datalist.count())
            return QVariant();
    const DataList &datalist = m_datalist[index.row()];

    if(role ==NameRole )
        return datalist.name();
    else if(role == ValueRole)
        return datalist.value();
    else if(role == CategoryRole)
        return datalist.category();

    return QVariant();
}

QHash<int, QByteArray> DataModel::roleNames() const {
    QHash<int, QByteArray> roles;
    roles[NameRole] = "name";
    roles[ValueRole] = "value";
    roles[CategoryRole] = "category";
    return roles;
}

void DataModel::onInitialPolygonAreaChanged(qreal ) {
    //0 is the index of iPA
    qDebug() << "IPA from model" ;
    QVector<int> roleVector;
    roleVector.append(ValueRole);
    roleVector.append(NameRole);
    roleVector.append(CategoryRole);
    roleVector.append(Qt::DisplayRole);       
    emit dataChanged(index(0),index(0),roleVector);
}

Mainwindow.cpp:

model.addData(DataList(QString("Initial Polygon Area"),QString::number(av1.initialPolygonArea()) + " sq.km",QString("Area")));

View->rootContext()->setContextProperty("AVModel",&model);               

connect(&av1,SIGNAL(initialPolygonAreaChanged(qreal)),&model,SLOT(onInitialPolygonAreaChanged(qreal)));

view.qml:

ListView {
    id: view
    anchors.top: parent.top
    anchors.bottom: parent.bottom

    width: parent.width
    model: AVModel
    delegate: Text { text: name + "     " + value; font.pixelSize: 18 ; color: "white"}
}

0 个答案:

没有答案