六、Qt widget应用、C++、qml混合编程之五————动态生成QML表格

本文介绍了如何使用Qt混合编程,动态生成QML表格并利用C++更新表格数据。关键步骤包括创建TableModel类,重写QAbstractTableModel方法,通过Qt信号与槽更新数据,并在QML中进行表格布局。示例代码详细展示了如何实现这一过程。
摘要由CSDN通过智能技术生成

关键词: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[

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值