Qt工作笔记-依赖于QAbstractTableModel实现自定义TableModel
发布日期:2021-06-30 10:58:46
浏览次数:2
分类:技术文章
本文共 3100 字,大约阅读时间需要 10 分钟。
目录
理论
设计模式里面有一句话叫 要依赖于抽象,不要依赖于具体。
但在Qt有些官方实例里,却依赖于具体实现某一子功能,这可以是为了方便把,
但在TableModel有一个依赖与抽象,
官方解释如下:
所以最少要实现这几个函数,才能跑起来://必须实现这3个 int rowCount(const QModelIndex &parent) const Q_DECL_OVERRIDE; int columnCount(const QModelIndex &parent) const Q_DECL_OVERRIDE; QVariant data(const QModelIndex &index, int role) const Q_DECL_OVERRIDE;
只有重写了这几个函数,虚表才能被覆盖,调用自己写的东西,实现自己想要的结果;
源码
程序运行截图如下:
程序结构如下:
源码如下:
mytableviewmodel.h
#ifndef MYTABLEVIEWMODEL_H#define MYTABLEVIEWMODEL_H#include#include class MyTableViewModel: public QAbstractTableModel{ Q_OBJECTpublic: MyTableViewModel(); //必须实现这3个 int rowCount(const QModelIndex &parent) const Q_DECL_OVERRIDE; int columnCount(const QModelIndex &parent) const Q_DECL_OVERRIDE; QVariant data(const QModelIndex &index, int role) const Q_DECL_OVERRIDE; //按照功能可选实现 QVariant headerData(int section, Qt::Orientation orientation, int role) const Q_DECL_OVERRIDE; //还可以实现如下的几个函数 //如:setData(),XXXX等函数 //自己添加的函数,方便管理 void addItem(QString one, QString two, QString three);private: //方便数据存储 QVector > m_aryData;};#endif // MYTABLEVIEWMODEL_H
widget.h
#ifndef WIDGET_H#define WIDGET_H#includenamespace Ui {class Widget;}class Widget : public QWidget{ Q_OBJECTpublic: explicit Widget(QWidget *parent = 0); ~Widget();private: Ui::Widget *ui;};#endif // WIDGET_H
main.cpp
#include "widget.h"#includeint main(int argc, char *argv[]){ QApplication a(argc, argv); Widget w; w.show(); return a.exec();}
mytableviewmodel.cpp
#include "mytableviewmodel.h"#includeMyTableViewModel::MyTableViewModel(): QAbstractTableModel(NULL){}int MyTableViewModel::rowCount(const QModelIndex &parent) const{ Q_UNUSED(parent) return m_aryData.size();}int MyTableViewModel::columnCount(const QModelIndex &parent) const{ Q_UNUSED(parent) return 3;}QVariant MyTableViewModel::data(const QModelIndex &index, int role) const{ if(role == 0) return m_aryData[index.row()][index.column()]; else return QVariant();}QVariant MyTableViewModel::headerData(int section, Qt::Orientation orientation, int role) const{ if(role == Qt::DisplayRole && orientation == Qt::Horizontal){ if(section == 0) return QStringLiteral("第一列"); else if(section == 1) return QStringList("第二列"); else if(section == 2) return QStringLiteral("第三列"); } return QVariant();}void MyTableViewModel::addItem(QString one, QString two, QString three){ beginInsertRows(QModelIndex(), m_aryData.size(),m_aryData.size()); QVector list; list << one << two << three; m_aryData << list; endInsertRows();}
widget.cpp
#include "widget.h"#include "ui_widget.h"#include "mytableviewmodel.h"#includeWidget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget){ ui->setupUi(this); MyTableViewModel *model = new MyTableViewModel; model->addItem("data1", "data2", "data3"); ui->tableView->setModel(model);}Widget::~Widget(){ delete ui;}
转载地址:https://it1995.blog.csdn.net/article/details/86064634 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
哈哈,博客排版真的漂亮呢~
[***.90.31.176]2024年04月29日 09时05分11秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
squid代理-----透明代理模式
2019-04-30
squid代理介绍----ACL控制应用+sarg日志分析+反向代理
2019-04-30
redis集群之主从模式+哨兵模式
2019-04-30
JavaScript原生开关灯效果
2019-04-30
企业邮箱如何申请注册,邮箱申请如何免费注册?
2019-04-30
微信企业邮箱,手机邮箱格式地址怎么写?
2019-04-30
公司如何申请企业邮箱,公司邮箱怎么申请,公司企业邮箱哪个好?
2019-04-30
电子邮箱账号怎么申请,怎样申请邮箱账号呢
2019-04-30
邮箱怎么发邮件,邮件发信量多少,职场新人怎么发汇报邮件呢?
2019-04-30
maven 多层次pom 新引入包,编译成功,还是没有将包引入到本地
2019-04-30
leetCode2 两数相加
2019-04-30
【工具使用】使用pip与conda安装、更新与卸载Pytorch和torchvision
2019-04-30
【工具使用】Google免费云环境Colaboratory使用
2019-04-30
【深度学习笔记】卷积层,全连接层,池化层的相关输出参数计算
2019-04-30
【NLP学习笔记】文本分类概述
2019-04-30
【深度学习笔记】文本分类
2019-04-30