qt5的mvc实现,model总共8列数据,代理其中第7列为icon+文本,model文本颜色可设置,ico在文本之前,请思考一下,给出c++示例代码

时间: 2024-03-20 22:43:06 浏览: 21
好的,这里是Qt5的MVC实现,其中第7列为icon+文本,model文本颜色可设置,icon在文本之前的C++示例代码: 首先,我们需要自定义一个代理来实现第7列的icon+文本的显示和设置文本颜色的功能。代码如下: ```cpp #include <QStyledItemDelegate> class MyDelegate : public QStyledItemDelegate { public: MyDelegate(QObject *parent = nullptr) : QStyledItemDelegate(parent) {} void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override { if (index.column() == 6) { // 第7列 QString text = index.data(Qt::DisplayRole).toString(); QIcon icon = qvariant_cast<QIcon>(index.data(Qt::DecorationRole)); QStyleOptionViewItem opt = option; initStyleOption(&opt, index); QRect iconRect = QRect(opt.rect.left() + 2, opt.rect.top() + 2, opt.rect.height() - 4, opt.rect.height() - 4); QRect textRect = QRect(iconRect.right() + 4, opt.rect.top(), opt.rect.width() - iconRect.width() - 6, opt.rect.height()); painter->save(); painter->setClipRect(opt.rect); painter->drawPixmap(iconRect, icon.pixmap(iconRect.size())); painter->setPen(index.data(Qt::TextColorRole).value<QColor>()); painter->drawText(textRect, Qt::AlignVCenter, text); painter->restore(); } else { QStyledItemDelegate::paint(painter, option, index); } } }; ``` 然后,我们需要自定义一个model来提供数据,代码如下: ```cpp #include <QAbstractTableModel> class MyTableModel : public QAbstractTableModel { public: MyTableModel(QObject *parent = nullptr) : QAbstractTableModel(parent) {} // 设置表头 QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override { if (orientation == Qt::Horizontal && role == Qt::DisplayRole) { switch (section) { case 0: return tr("ID"); case 1: return tr("Name"); case 2: return tr("Age"); case 3: return tr("Gender"); case 4: return tr("Address"); case 5: return tr("Phone"); case 6: return tr("Info"); case 7: return tr("Remark"); default: break; } } return QVariant(); } // 设置数据 QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override { if (role == Qt::DisplayRole) { switch (index.column()) { case 0: return QString::number(index.row() + 1); case 1: return QString("Name%1").arg(index.row() + 1); case 2: return QString::number(20 + index.row()); case 3: return index.row() % 2 == 0 ? tr("Male") : tr("Female"); case 4: return tr("Address%1").arg(index.row() + 1); case 5: return tr("Phone%1").arg(index.row() + 1); case 6: return QVariant::fromValue(QIcon(":/icon.png"), QString("Info%1").arg(index.row() + 1)); case 7: return tr("Remark%1").arg(index.row() + 1); default: break; } } else if (role == Qt::TextColorRole && index.column() == 7) { return QColor(Qt::red); } else if (role == Qt::DecorationRole && index.column() == 6) { return QVariant::fromValue(QIcon(":/icon.png")); } return QVariant(); } // 设置行数 int rowCount(const QModelIndex &parent = QModelIndex()) const override { return 10; } // 设置列数 int columnCount(const QModelIndex &parent = QModelIndex()) const override { return 8; } }; ``` 最后,我们在主窗口中使用自定义的代理和model来显示数据,代码如下: ```cpp #include <QTableView> #include <QVBoxLayout> #include "mydelegate.h" #include "mytablemodel.h" class MainWindow : public QMainWindow { public: MainWindow(QWidget *parent = nullptr) : QMainWindow(parent) { QTableView *tableView = new QTableView(this); tableView->setItemDelegateForColumn(6, new MyDelegate(tableView)); tableView->setModel(new MyTableModel(tableView)); QVBoxLayout *layout = new QVBoxLayout; layout->addWidget(tableView); QWidget *widget = new QWidget(this); widget->setLayout(layout); setCentralWidget(widget); } }; ``` 这样就完成了Qt5的MVC实现,其中第7列为icon+文本,model文本颜色可设置,icon在文本之前的功能。

相关推荐

最新推荐

recommend-type

HTML5中在title标题标签里设置小图标的方法

link href="images/logo.ico" rel="icon" type="image/x-ico"&gt; &lt;link href="style.css" rel="stylesheet" type="text/css"&gt; &lt;/head&gt; 在这里简单说明一下: 但凡遇到自己不熟悉的东西时,其实都...
recommend-type

vue3修改link标签默认icon无效问题详解

主要介绍了vue3修改link标签默认icon无效问题详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
recommend-type

ICO(控制反转) C#实现

IoC的全名是『Inversion of Control』,字面上的意思是『控制反转』,要了解...一般来说,当设计师撰写一个Console程序时, 控制权是在该程序上,它决定着何时该印出讯息、何时又该接受使用者输入、何时该进行数据处理
recommend-type

wxpython+pymysql实现用户登陆功能

wxpython最为一款python GUI库,由于简单和轻便外加强大的功能而受到很多python爱好者的喜爱,...#其中参数type表示图片的类型,还有ico,jpgm等类型 icon_1 = wx.Icon(name='python1.png',type=wx.BITMAP_TYPE_PNG)
recommend-type

C# 读取ttf字体文件里的Unicode实现

因为爬虫要解析 &#x880cc这种字体编码的值,下载到一个ttf文件,用 百度字体编辑器 打开,可以看到每个字符对应的Unicode (数字下方 $23、$2A…这些), 我需要拿到这些映射关系,才能解析对应数字。...
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

MATLAB柱状图在信号处理中的应用:可视化信号特征和频谱分析

![matlab画柱状图](https://img-blog.csdnimg.cn/3f32348f1c9c4481a6f5931993732f97.png) # 1. MATLAB柱状图概述** MATLAB柱状图是一种图形化工具,用于可视化数据中不同类别或组的分布情况。它通过绘制垂直条形来表示每个类别或组中的数据值。柱状图在信号处理中广泛用于可视化信号特征和进行频谱分析。 柱状图的优点在于其简单易懂,能够直观地展示数据分布。在信号处理中,柱状图可以帮助工程师识别信号中的模式、趋势和异常情况,从而为信号分析和处理提供有价值的见解。 # 2. 柱状图在信号处理中的应用 柱状图在信号处理
recommend-type

用Spring boot和vue写一个登录注册界面

好的,这是一个比较常见的技术问题,我可以为您提供一些参考资料和思路。首先,您可以使用Spring Boot作为后端框架来处理用户认证和注册的逻辑,比如使用Spring Security实现用户登录认证。同时,您还需要设计相应的数据模型和数据库表结构来存储用户信息。在前端方面,您可以使用Vue.js作为框架来构建登录注册页面,使用Axios来发起API请求并和后端进行交互。当然,在实现过程中,还需要考虑一些具体细节,比如数据校验、安全性和用户体验等方面。希望这些信息能够帮助到您。
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。