关键词:TableView,TableViewColumn,timerEvent
1、建立一个中间层TableModel类,更新qml图表的数据在这个类中完成,该类继承自QAbstractTableModel,主要是重写QAbstractTableModel类的如下方法:
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
int columnCount(const QModelIndex &parent = QModelIndex()) const override;
QVariant data(const QModelIndex &index,int role=Qt::DisplayRole) const override;
QHash<int,QByteArray> roleNames() const override;
2、TableModel.cpp代码如下:
#include "tablemodel.h"
#include "l_global.h"
TableModel::TableModel(QObject *parent) : QAbstractTableModel(parent)
{
}
TableModel::~TableModel()
{
}
int TableModel::rowCount(const QModelIndex &parent) const{
Q_UNUSED(parent)
return m_aryData.size();
}
int TableModel::columnCount(const QModelIndex &parent) const{
Q_UNUSED(parent)
return 3;
}
QVariant TableModel::data(const QModelIndex &index, int role) const{
if(index.column()<0||columnCount()<=index.column()||
index.row()<0||rowCount()<=index.row())
{
return QVariant();
}
switch(role)
{
case role1:
return m_aryData[index.row()].at(0);
case role2:
return m_aryData[index.row()].at(1);
case role3:
return m_aryData[index.row()].at(2);
case role4:
return m_aryData[index.row()].at(3);
case role5:
return m_aryData[index.row()].at(4);
case role6:
return m_aryData[index.row()].at(5);
case role7:
return m_aryData[index.row()].at(6);
case role8:
return m_aryData[index.row()].at(7);
case role9:
return m_aryData[index.row()].at(8);
case role10:
return m_aryData[index.row()].at(9);
case role11:
return m_aryData[index.row()].at(10);
case role12:
return m_aryData[index.row()].at(11);
case role13:
return m_aryData[index.row()].at(12);
case role14:
return m_aryData[index.row()].at(13);
case role15:
return m_aryData[index.row()].at(14);
case role16:
return m_aryData[index.row()].at(15);
case role17:
return m_aryData[index.row()].at(16);
case role18:
return m_aryData[