Qt之MVC架构MVD

news/2025/3/25 11:34:11/

什么是MVC架构

MVC模式(Model–view–controller)是软件工程中的一种软件架构模式,把软件系统分为三个基本部分:模型(Model)、视图(View)和控制器(Controller)。

MVC模式最早由Trygve Reenskaug在1978年提出[1],是施乐帕罗奥多研究中心(Xerox PARC)在20世纪80年代为程序语言Smalltalk发明的一种软件架构。MVC模式的目的是实现一种动态的程序设计,使后续对程序的修改和扩展简化,并且使程序某一部分的重复利用成为可能。除此之外,此模式透过对复杂度的简化,使程序结构更加直观。软件系统透过对自身基本部分分离的同时也赋予了各个基本部分应有的功能。专业人员可以依据自身的专长分组:

模型(Model) - 程序员编写程序应有的功能(实现算法等等)、数据库专家进行数据管理和数据库设计(可以实现具体的功能)。

视图(View) - 界面设计人员进行图形界面设计。

控制器(Controller)- 负责转发请求,对请求进行处理。

Qt的MVD架构又是什么?

模型(Model) - 程序员编写程序应有的功能(实现算法等等)、数据库专家进行数据管理和数据库设计(可以实现具体的功能)。

视图(View) - 界面设计人员进行图形界面设计。

代理(Delegate)- 负责转发请求,对请求进行处理

UI样式实现:

其实你就只要去实现下面的代码,你就能完全使用MVD架构,随心所欲

class TableModel : public QAbstractTableModel
{Q_OBJECT
public:enum Columns { Element = 0, Value, Count = Value + 1 };explicit TableModel(QObject *parent = nullptr);Qt::ItemFlags flags(const QModelIndex &index) const;int rowCount(const QModelIndex &parent = QModelIndex()) const;int columnCount(const QModelIndex &parent = QModelIndex()) const;QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::DisplayRole);void addElement(const QString &element, int value);private:};
class TableDelegate : public QStyledItemDelegate
{Q_OBJECT
public:explicit TableDelegate(QObject *parent = nullptr);QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;void setEditorData(QWidget *editor, const QModelIndex &index) const;void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const;void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const;
};//--------------------cpp---------------------
#include "tablemodel.h"TableModel::TableModel(QObject *parent) : QAbstractTableModel(parent)
{
}Qt::ItemFlags TableModel::flags(const QModelIndex &index) const
{return QAbstractTableModel::flags(index);
}int TableModel::rowCount(const QModelIndex &parent) const
{return 2;
}int TableModel::columnCount(const QModelIndex &parent) const
{return Count;
}QVariant TableModel::data(const QModelIndex &index, int role) const
{return QVariant();
}QVariant TableModel::headerData(int section, Qt::Orientation orientation, int role) const
{return QAbstractTableModel::headerData(section, orientation, role);
}bool TableModel::setData(const QModelIndex &index, const QVariant &value, int role)
{if (!index.isValid() || index.row() < 0 || index.row() >= mElements.count()){return false;}return false;
}
void TableModel::addElement(const QString &element, int value)
{
}TableDelegate::TableDelegate(QObject *parent) : QStyledItemDelegate(parent)
{
}QWidget *TableDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option,const QModelIndex &index) const
{return QStyledItemDelegate::createEditor(parent, option, index);
}void TableDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{QStyledItemDelegate::setEditorData(editor, index);
}void TableDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{QStyledItemDelegate::setModelData(editor, model, index);
}void TableDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{QStyledItemDelegate::paint(painter, option, index);
}void TableDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option,const QModelIndex &index) const
{Q_UNUSED(index)editor->setGeometry(option.rect);
}

Model

class QAbstractItemModel {virtual QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const = 0;virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const = 0;virtual bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) = 0;virtual int rowCount(const QModelIndex &parent = QModelIndex()) const = 0;virtual int columnCount(const QModelIndex &parent = QModelIndex()) const = 0;virtual Qt::ItemFlags flags(const QModelIndex &index) const = 0;
};

实现上面的代码,你就能使用MVD架构,理解Qt的框架,

View 与 Model 的绑定流程

当调用 view->setModel(model)时:
  1. 存储指针:View 内部保存QAbstractItemModel*类型的指针指向用户的 Model 对象
  2. 信号连接:View 自动连接 Model 的信号(如 dataChanged())到自身的槽(如 update()),实现数据变化时的视图更新
  3. 数据请求:当视图需要渲染时,会通过 Model 指针调用虚函数(如data()rowCount())
// 伪代码:QAbstractItemView 的渲染流程
void QAbstractItemView::paintEvent() {for (int row = 0; row < model->rowCount(); ++row) { // 多态调用 model->rowCount()QVariant data = model->data(index(row));        // 多态调用 model->data()delegate->paint(painter, data);                 // 多态调用 delegate->paint()}
}

Delegate 的动态调用机制

当设置 view->setItemDelegate(delegate)  后:
  1. 渲染阶段:View 在绘制每个数据项时,调用delegate->paint(painter, option, index)
  2. 编辑阶段:当用户双击单元格时,调用delegate->createEditor()创建编辑器
  3. 数据回写:编辑器关闭时,调用delegate->setModelData()将数据写回 Model
// 伪代码:委托的编辑器创建过程
void QAbstractItemView::edit(const QModelIndex &index) {QWidget* editor = delegate->createEditor(parent, option, index); // 多态调用connect(editor, &QWidget::destroyed, [this, index]() {delegate->setModelData(editor, model, index); // 多态调用});
}


http://www.ppmy.cn/news/1582533.html

相关文章

Git更改暂存 : Git Pull 错误的快速解决方法

你是否遇到过在运行 git pull 时出现如下错误&#xff1f; error: cannot pull with rebase: You have unstaged changes. error: please commit or stash them.该消息表明 Git 检测到你的工作目录中存在尚未暂存或提交的修改。为了防止在执行 pull 操作时产生冲突或潜在的数据…

关于误差平面小记

四维曲面的二维切片&#xff1a;误差平面详解 在深度学习优化过程中&#xff0c;我们通常研究损失函数&#xff08;Loss Function&#xff09;的变化&#xff0c;试图找到权重的最优配置。由于神经网络的参数空间通常是高维的&#xff0c;我们需要使用低维可视化的方法来理解优…

关于网络的一点知识(持续更新)

1、IP地址和子网掩码、端口号: IP地址是设备在网络上的地址,相当于一栋房子的门牌号。子网掩码相当于房子所在的街道。同一条街道的房子间是通过街道直通的,主人可以互相拜访。 举个例子,如下图所示。 说明:将两台设备的IP和子网掩码转化为二进制,然后将各自的IP地址和…

计算机网络基础之三种交换技术及其性能分析

一. 交换技术基础 1. 三种交换技术 电路交换&#xff1a;用于电话网络报文交换&#xff1a;用于电报网络分组交换&#xff1a;用于现代计算机网络 2. 人类历史上的通信网络 #mermaid-svg-AeGvrkUbCkicFOIo {font-family:"trebuchet ms",verdana,arial,sans-serif;…

STM32标准库之I2C示例代码

软件I2C读写MPU6050 MPU6050地址0xD0(写)&#xff0c;读&#xff1a;0xD0 | 0x01 MyI2C.c #include "stm32f10x.h" // Device header #include "Delay.h"/*引脚配置层*//*** 函 数&#xff1a;I2C写SCL引脚电平* 参 数&#xff…

Vue秘籍:如何动态修改页面 Title(浏览器页签名称)?

Vue秘籍&#xff1a;如何动态修改页面 Title&#xff08;浏览器页签名称&#xff09;&#xff1f; 在开发 Vue 项目时&#xff0c;我们经常需要根据不同的页面动态修改浏览器的页签标题&#xff08;title&#xff09;&#xff0c;比如&#xff1a; 在电商网站中&#xff0c;展…

计算机网络高频(二)TCP/IP基础

计算机网络高频(二)TCP/IP基础 1.什么是TCP/IP⭐⭐ TCP/IP是一种网络通信协议,它是互联网中最常用的协议之一。TCP/IP有两个基本的协议:TCP(传输控制协议)和IP(互联网协议)。 TCP(Transmission Control Protocol,传输控制协议)是一种可靠的、面向连接的协议。它负…

蓝桥杯 之 第27场月赛总结

文章目录 习题1.抓猪拿国一2.蓝桥字符3.蓝桥大使4.拳头对决5.未来竞赛6.备份比赛数据 习题 比赛地址 1.抓猪拿国一 十分简单的签到题 print(sum(list(range(17))))2.蓝桥字符 常见的字符匹配的问题&#xff0c;是一个二维dp的问题&#xff0c;转化为对应的动态规划求解 力扣…