Qt ListView 刷新数据

在之前的例子中做一些修改,首先不显示ListViewModel数据,当点击鼠标后,显示数据。

几个要点:

用Q_INVOKABLE 创建C++的方法,这样可以在Qml中直接调用

清除model里面的数据必须用beginResetModel和endResetModel方法将清除代码包起来。

看一下完整的代码:

test.qml

import QtQuick 2.0
import mylist 1.0

ListView {
    width: 180; height: 200

    model: PeopleListModel {
        id: model1
    }

    delegate: Text {
        text: name + ": " + number
    }

    MouseArea {
        anchors.fill: parent
        onClicked: model1.reload();
    }
    
}

plugin.cpp

#include <QtQml/QQmlExtensionPlugin>
#include <QtQml/qqml.h>
#include <qdebug.h>
#include <qdatetime.h>
#include <qbasictimer.h>
#include <qcoreapplication.h>
#include <QAbstractItemModel>
#include <QStringList>
#include <iostream>


class People {
public:
  People(QString const & name, QString const & number)
    : name_(name), number_(number) {
  }

  QString name() const {
    return name_;
  }

  QString number() const {
    return number_;
  }

private:
  QString name_;
  QString number_;

};

class PeopleListModel : public QAbstractListModel {
  Q_OBJECT

public:
  enum PeopleRoles {
    NameRole = Qt::UserRole + 1,
    NumberRole
  };

  PeopleListModel(QObject * parent = 0)
    : QAbstractListModel(parent) {
    /*
    People p1("Dean", "186***");
    addPeople(p1);

    People p2("Crystal", "186***");
    addPeople(p2);*/
  }

  void addPeople(People const & p) {
    beginInsertRows(QModelIndex(), rowCount(), rowCount());
    values_ << p;
    endInsertRows();
  }

  int rowCount(QModelIndex const & parent = QModelIndex()) const {
    return values_.count();
  }

  QVariant data(QModelIndex const & index, int role = Qt::DisplayRole) const {
    std::cout << "data()" << std::endl;
    std::cout << "index row: " << index.row() << " index column: " << index.column() << std::endl;
    if (index.row() < 0 || index.row() >= values_.count()) {
      std::cout << "s1" << std::endl;
      return QVariant();
    }

    People const & p = values_[index.row()];
    std::cout << "role: " << role << std::endl;
    if (role == NameRole) {

      return p.name();
    }
    else if (role == NumberRole) {
      return p.number();
    }
    return QVariant();
  }

  Q_INVOKABLE void reload() {
    std::cout << "--------------------" << std::endl;
    std::cout << "reload()" << std::endl;
    beginResetModel();
    values_.clear();
    endResetModel();
    People p1("Dean", "176***");
    addPeople(p1);

    People p2("Crystal", "176***");
    addPeople(p2);
    std::cout << "values count: " << values_.count() << std::endl;
  }


protected:
  QHash<int, QByteArray> roleNames() const {
    QHash<int, QByteArray> roles;
    roles[NameRole] = "name";
    roles[NumberRole] = "number";
    return roles;
  }

private:
  QList<People> values_;
};

class QExampleQmlPlugin : public QQmlExtensionPlugin {
  Q_OBJECT
  Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QQmlExtension.PeopleListModel")

  public:
  void registerTypes(char const * uri) {
    qmlRegisterType<PeopleListModel>(uri, 1, 0, "PeopleListModel");
  }
};

#include "plugin.moc"

运行效果,刚开始什么都没有,点击后出现数据。

Qt ListView 刷新数据



Qt ListView 刷新数据

上一篇:textField使用合集(附字体设置合集)


下一篇:最长有效小括弧(Longest Valid Parentheses)