Qt 继承QAbstractTableModel实现自定义TableModel

2023-11-07 01:36

本文主要是介绍Qt 继承QAbstractTableModel实现自定义TableModel,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1.简介

QAbstractTableModel为将数据表示为二维项数组的模型提供了一个标准接口。它不直接使用,但必须进行子类化。

由于该模型提供了比QAbstractItemModel更专业的接口,因此它不适合与树视图一起使用,尽管它可以用于向QListView提供数据。如果需要表示一个简单的项列表,并且只需要一个模型包含一列数据,那么将QAbstractListModel子类化可能更合适。

继承QAbstractTableModel,需要重写rowCount()、columnCount()、data()和headerData()等函数。

  • rowCount()函数返回模型的行数。
  • columnCount()函数返回模型的列数。
  • data()函数返回指定索引处的数据。
  • headerData()函数返回表头的数据,根据orientation参数返回水平或垂直表头的数据。

2.示例

#ifndef MYTABLEMODEL_H
#define MYTABLEMODEL_H#include <QAbstractTableModel>
#include <QObject>
#include <QList>typedef struct _student
{QString name;int age;double score;
}Student;class MyTableModel : public QAbstractTableModel
{Q_OBJECT
public:MyTableModel(QObject *parent = nullptr);enum RoleNames{Name,Age,Score};public://更新void update(QList<Student> students);//行数量virtual int rowCount(const QModelIndex &parent = QModelIndex()) const;//列数量virtual int columnCount(const QModelIndex &parent = QModelIndex()) const;// 表格项数据virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;// 表头数据virtual QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;private:QList<Student> m_lstStu;
};#endif // MYMODEL_H#include "MyTableModel.h"MyTableModel::MyTableModel(QObject *parent): QAbstractTableModel(parent)
{}void MyTableModel::update(QList<Student> students)
{m_lstStu = students;for(int i=0;i<m_lstStu.size();i++){beginInsertRows(QModelIndex(),i,i);endInsertRows();}
}int MyTableModel::rowCount(const QModelIndex &parent) const
{Q_UNUSED(parent);return m_lstStu.count();
}int MyTableModel::columnCount(const QModelIndex &parent) const
{Q_UNUSED(parent);return 3;
}QVariant MyTableModel::data(const QModelIndex &index, int role) const
{if (!index.isValid())return QVariant();int nColumn = index.column();int nRow = index.row();Student stu = m_lstStu.at(nRow);if(role == Qt::DisplayRole){if (nColumn == MyTableModel::Name)return stu.name;else if(nColumn == MyTableModel::Age)return stu.age;else if(nColumn == MyTableModel::Score)return stu.score;}return QVariant();
}QVariant MyTableModel::headerData(int section, Qt::Orientation orientation, int role) const
{Q_UNUSED(section);if(orientation == Qt::Horizontal && role == Qt::DisplayRole){if (section == MyTableModel::Name)return QStringLiteral("姓名");else if(section == MyTableModel::Age)return QStringLiteral("年龄");else if(section == MyTableModel::Score)return QStringLiteral("分数");}return QVariant();
}

使用:

    //去除选中虚线框ui->tableView->setFocusPolicy(Qt::NoFocus);//设置最后一栏自适应长度ui->tableView->horizontalHeader()->setStretchLastSection(true);//设置整行选中ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);//不显示垂直表头ui->tableView->verticalHeader()->setVisible(false);MyTableModel *pModel = new MyTableModel(this);// 构造数据,更新界面QList<Student> students;QList<QString> nameList;nameList<<"张三"<<"李四"<<"王二"<<"赵五"<<"刘六";for (int i = 0; i < 5; ++i){Student student;student.name = nameList.at(i);student.age = qrand()%6 + 13;//随机生成13到19的随机数student.score = qrand()%20 + 80;//随机生成0到100的随机数;students.append(student);}pModel->update(students);ui->tableView->setModel(pModel);

这篇关于Qt 继承QAbstractTableModel实现自定义TableModel的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/360358

相关文章

Java实现视频格式转换的完整指南

《Java实现视频格式转换的完整指南》在Java中实现视频格式的转换,通常需要借助第三方工具或库,因为视频的编解码操作复杂且性能需求较高,以下是实现视频格式转换的常用方法和步骤,需要的朋友可以参考下... 目录核心思路方法一:通过调用 FFmpeg 命令步骤示例代码说明优点方法二:使用 Jaffree(FF

基于C#实现MQTT通信实战

《基于C#实现MQTT通信实战》MQTT消息队列遥测传输,在物联网领域应用的很广泛,它是基于Publish/Subscribe模式,具有简单易用,支持QoS,传输效率高的特点,下面我们就来看看C#实现... 目录1、连接主机2、订阅消息3、发布消息MQTT(Message Queueing Telemetr

Java实现图片淡入淡出效果

《Java实现图片淡入淡出效果》在现代图形用户界面和游戏开发中,**图片淡入淡出(FadeIn/Out)**是一种常见且实用的视觉过渡效果,它可以用于启动画面、场景切换、轮播图、提示框弹出等场景,通过... 目录1. 项目背景详细介绍2. 项目需求详细介绍2.1 功能需求2.2 非功能需求3. 相关技术详细

Python实现获取带合并单元格的表格数据

《Python实现获取带合并单元格的表格数据》由于在日常运维中经常出现一些合并单元格的表格,如果要获取数据比较麻烦,所以本文我们就来聊聊如何使用Python实现获取带合并单元格的表格数据吧... 由于在日常运维中经常出现一些合并单元格的表格,如果要获取数据比较麻烦,现将将封装成类,并通过调用list_exc

使用animation.css库快速实现CSS3旋转动画效果

《使用animation.css库快速实现CSS3旋转动画效果》随着Web技术的不断发展,动画效果已经成为了网页设计中不可或缺的一部分,本文将深入探讨animation.css的工作原理,如何使用以及... 目录1. css3动画技术简介2. animation.css库介绍2.1 animation.cs

Java进行日期解析与格式化的实现代码

《Java进行日期解析与格式化的实现代码》使用Java搭配ApacheCommonsLang3和Natty库,可以实现灵活高效的日期解析与格式化,本文将通过相关示例为大家讲讲具体的实践操作,需要的可以... 目录一、背景二、依赖介绍1. Apache Commons Lang32. Natty三、核心实现代

SpringBoot实现接口数据加解密的三种实战方案

《SpringBoot实现接口数据加解密的三种实战方案》在金融支付、用户隐私信息传输等场景中,接口数据若以明文传输,极易被中间人攻击窃取,SpringBoot提供了多种优雅的加解密实现方案,本文将从原... 目录一、为什么需要接口数据加解密?二、核心加解密算法选择1. 对称加密(AES)2. 非对称加密(R

基于Go语言实现Base62编码的三种方式以及对比分析

《基于Go语言实现Base62编码的三种方式以及对比分析》Base62编码是一种在字符编码中使用62个字符的编码方式,在计算机科学中,,Go语言是一种静态类型、编译型语言,它由Google开发并开源,... 目录一、标准库现状与解决方案1. 标准库对比表2. 解决方案完整实现代码(含边界处理)二、关键实现细

python通过curl实现访问deepseek的API

《python通过curl实现访问deepseek的API》这篇文章主要为大家详细介绍了python如何通过curl实现访问deepseek的API,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编... API申请和充值下面是deepeek的API网站https://platform.deepsee

SpringBoot实现二维码生成的详细步骤与完整代码

《SpringBoot实现二维码生成的详细步骤与完整代码》如今,二维码的应用场景非常广泛,从支付到信息分享,二维码都扮演着重要角色,SpringBoot是一个非常流行的Java基于Spring框架的微... 目录一、环境搭建二、创建 Spring Boot 项目三、引入二维码生成依赖四、编写二维码生成代码五