在QTableView中使用各种自定义委托

QT的MVC(View/Delegate)模型十分强大,可以利用各种控件来对表格的输入进行限制,不过我以前一直没有过,这几天研究了一下,写个小例子,希望大家喜欢。

    如果看不懂这个例子,请先看QT的自带例子:http://qt-project.org/doc/qt-4.8/itemviews-spinboxdelegate.html

思路:

1:为每一列定义委托:
A:第一列是编号列,使用只读委托,令该列的单元格是只读的
B:第三列是ID列,只能输入1-12个数字,利用QLineEdit委托和正则表达式对输入进行限制
C:第四年龄列,利用QSpinBox委托进行输入限制,只能输入1-100之间的数字
D:第五列是性别列,利用QComboBox委托对输入进行限制,该列的单元格只能输入Male或Female
E:第六列是头像列,在该列的单元格中央放置一张头像

2:定义代理类,把所有单元格中的字符居中显示。

3:利用QSS,将表格的背景色弄成黄蓝相间。

截图:

上代码:

    1. #include <QtGui>  
    2.   
    3. //编号列,只读委托  
    4. //这个方法我还真想不到,呵呵  
    5. class ReadOnlyDelegate : public QItemDelegate  
    6. {  
    7.     Q_OBJECT  
    8. public:  
    9.     ReadOnlyDelegate(QObject *parent = 0): QItemDelegate(parent) { }  
    10.     QWidget *createEditor(QWidget*parent, const QStyleOptionViewItem &option,  
    11.         const QModelIndex &index) const  
    12.     {  
    13.         return NULL;  
    14.     }  
    15. };  
    16.   
    17. //ID列,只能输入1-12个数字  
    18. //利用QLineEdit委托和正则表达式对输入进行限制  
    19. class UserIDDelegate : public QItemDelegate  
    20. {  
    21.     Q_OBJECT  
    22. public:  
    23.     UserIDDelegate(QObject *parent = 0): QItemDelegate(parent) { }  
    24.     QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,  
    25.         const QModelIndex &index) const  
    26.     {  
    27.         QLineEdit *editor = new QLineEdit(parent);  
    28.         QRegExp regExp("[0-9]{0,10}");  
    29.         editor->setValidator(new QRegExpValidator(regExp, parent));  
    30.         return editor;  
    31.     }  
    32.     void setEditorData(QWidget *editor, const QModelIndex &index) const  
    33.     {  
    34.         QString text = index.model()->data(index, Qt::EditRole).toString();  
    35.         QLineEdit *lineEdit = static_cast<QLineEdit*>(editor);  
    36.         lineEdit->setText(text);  
    37.     }  
    38.     void setModelData(QWidget *editor, QAbstractItemModel *model,  
    39.         const QModelIndex &index) const  
    40.     {  
    41.         QLineEdit *lineEdit = static_cast<QLineEdit*>(editor);  
    42.         QString text = lineEdit->text();  
    43.         model->setData(index, text, Qt::EditRole);  
    44.     }  
    45.     void updateEditorGeometry(QWidget *editor,  
    46.         const QStyleOptionViewItem &option, const QModelIndex &index) const  
    47.     {  
    48.         editor->setGeometry(option.rect);  
    49.     }  
    50. };  
    51.   
    52. //年龄列,利用QSpinBox委托进行输入限制,只能输入1-100之间的数字  
    53. class AgeDelegate : public QItemDelegate  
    54. {  
    55.     Q_OBJECT  
    56. public:  
    57.     AgeDelegate(QObject *parent = 0): QItemDelegate(parent) { }  
    58.     QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,  
    59.         const QModelIndex &index) const  
    60.     {  
    61.         QSpinBox *editor = new QSpinBox(parent);  
    62.         editor->setMinimum(1);  
    63.         editor->setMaximum(100);  
    64.         return editor;  
    65.     }  
    66.     void setEditorData(QWidget *editor, const QModelIndex &index) const  
    67.     {  
    68.         int value = index.model()->data(index, Qt::EditRole).toInt();  
    69.         QSpinBox *spinBox = static_cast<QSpinBox*>(editor);  
    70.         spinBox->setValue(value);  
    71.     }  
    72.     void setModelData(QWidget *editor, QAbstractItemModel *model,  
    73.         const QModelIndex &index) const  
    74.     {  
    75.         QSpinBox *spinBox = static_cast<QSpinBox*>(editor);  
    76.         spinBox->interpretText();  
    77.         int value = spinBox->value();  
    78.         model->setData(index, value, Qt::EditRole);  
    79.     }  
    80.     void updateEditorGeometry(QWidget *editor,  
    81.         const QStyleOptionViewItem &option, const QModelIndex &index) const  
    82.     {  
    83.         editor->setGeometry(option.rect);  
    84.     }  
    85. };  
    86.   
    87. //性别列,利用QComboBox委托对输入进行限制  
    88. //这一列的单元格只能输入Male或Female  
    89. class SexDelegate : public QItemDelegate  
    90. {  
    91.     Q_OBJECT  
    92. public:  
    93.     SexDelegate(QObject *parent = 0): QItemDelegate(parent) { }  
    94.     QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,  
    95.         const QModelIndex &index) const  
    96.     {  
    97.         QComboBox *editor = new QComboBox(parent);  
    98.         editor->addItem("Female");  
    99.         editor->addItem("Male");  
    100.         return editor;  
    101.     }  
    102.     void setEditorData(QWidget *editor, const QModelIndex &index) const  
    103.     {  
    104.         QString text = index.model()->data(index, Qt::EditRole).toString();  
    105.         QComboBox *comboBox = static_cast<QComboBox*>(editor);  
    106.         int tindex = comboBox->findText(text);  
    107.         comboBox->setCurrentIndex(tindex);  
    108.     }  
    109.     void setModelData(QWidget *editor, QAbstractItemModel *model,  
    110.         const QModelIndex &index) const  
    111.     {  
    112.         QComboBox *comboBox = static_cast<QComboBox*>(editor);  
    113.         QString text = comboBox->currentText();  
    114.         model->setData(index, text, Qt::EditRole);  
    115.     }  
    116.     void updateEditorGeometry(QWidget *editor,  
    117.         const QStyleOptionViewItem &option, const QModelIndex &index) const  
    118.     {  
    119.         editor->setGeometry(option.rect);  
    120.     }  
    121. };  
    122.   
    123. //头像列,只是在单元格中央放一张小图而已  
    124. class IconDelegate : public QItemDelegate  
    125. {  
    126.     Q_OBJECT  
    127. public:  
    128.     IconDelegate(QObject *parent = 0): QItemDelegate(parent) { }  
    129.     void paint(QPainter *painter, const QStyleOptionViewItem &option,  
    130.         const QModelIndex & index ) const  
    131.     {  
    132.         //show.bmp是在工程目录中的一张图片(其实就是QQ的图标啦,呵呵)  
    133.         QPixmap pixmap = QPixmap("show.bmp").scaled(24, 24);  
    134.         qApp->style()->drawItemPixmap(painter, option.rect,  Qt::AlignCenter, QPixmap(pixmap));  
    135.     }  
    136. };  
    137.   
    138. //代理类,把所有单元格中的字符居中显示  
    139. class VIPModel : public QStandardItemModel  
    140. {  
    141.     Q_OBJECT  
    142. public:  
    143.     VIPModel(QObject *parent=NULL) : QStandardItemModel(parent) { }  
    144.     VIPModel(int row, int column, QObject *parent=NULL)  
    145.         : QStandardItemModel(row, column, parent) { }  
    146.     QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const  
    147.     {  
    148.         if( Qt::TextAlignmentRole == role )  
    149.             return Qt::AlignCenter;   
    150.         return QStandardItemModel::data(index, role);  
    151.     }  
    152.   
    153. };  
    154.   
    155. #include "main.moc"  
    156.   
    157. int main(int argc, char *argv[])  
    158. {  
    159.     QApplication app(argc, argv);  
    160.   
    161.     VIPModel *model = new VIPModel(5, 5);  
    162.     QTableView *tableView = new QTableView;  
    163.   
    164.     //把表格的背景调成黄蓝相间  
    165.     //这种方法是在网上看到的,用起来还真方便啊  
    166.     tableView->setAlternatingRowColors(true);  
    167.     tableView->setStyleSheet("QTableView{background-color: rgb(250, 250, 115);"  
    168.         "alternate-background-color: rgb(141, 163, 215);}");  
    169.   
    170.     tableView->setWindowTitle("VIP List");  
    171.     tableView->resize(700, 400);  
    172.     tableView->setModel(model);  
    173.     QStringList headerList;  
    174.     headerList << "No." << "ID" << "Name" << "Age" << "Sex" << "Show";  
    175.     model->setHorizontalHeaderLabels(headerList);  
    176.     tableView->verticalHeader()->setVisible(false);  
    177.     tableView->horizontalHeader()->setStretchLastSection(true);  
    178.   
    179.     //为每一列加载委托  
    180.     ReadOnlyDelegate readOnlyDelegate;  
    181.     tableView->setItemDelegateForColumn(0, &readOnlyDelegate);  
    182.     UserIDDelegate userIDDelegate;  
    183.     tableView->setItemDelegateForColumn(1, &userIDDelegate);  
    184.     AgeDelegate spinBoxDelegate;  
    185.     tableView->setItemDelegateForColumn(3, &spinBoxDelegate);  
    186.     SexDelegate comboBoxDelegate;  
    187.     tableView->setItemDelegateForColumn(4, &comboBoxDelegate);  
    188.     IconDelegate iconDelegate;  
    189.     tableView->setItemDelegateForColumn(5, &iconDelegate);  
    190.   
    191.     for(int i=0; i<10; i++)  
    192.     {  
    193.         QModelIndex index = model->index(i, 0, QModelIndex());  
    194.         model->setData(index, i);  
    195.     }  
    196.   
    197.     tableView->show();  
    198.     return app.exec();  
    199. }  

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/207779.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

带删除的并查集

Almost Union-Find 支持三种操作 合并 x x x和 y y y所在的集合把 x x x移到 y y y所在的集合求 x x x所在的集合的元素个数和元素之和 操作1和3是基本的并查集的操作. 关键在于操作 2 2 2: 若使用朴素的并查集&#xff0c;把节点 1 1 1合并到 3 3 3所在的集合&#xff0c;会…

List系列集合

List系列集合特点&#xff1a;有序&#xff0c;可重复&#xff0c;有索引 ArrayList&#xff1a;有序&#xff0c;可重复&#xff0c;有索引 LinkedList&#xff1a;有序&#xff0c;可重复&#xff0c;有索引 &#xff08;底层实现不同&#xff01;适合的场景不同&#xff01;…

TZOJ 1402 Bitset

答案&#xff1a; #include <stdio.h> int main() {int n 0, j 0; while (scanf("%d", &n) ! EOF && (n>0 && n<1000)) //多组输入{int arr[32], i 0;while (n > 0) {arr[i] n % 2; //除2取余法n / 2;}for (j i -…

接口自动化测试思路和实战之模块化测试脚本框架

模块化测试脚本框架 需要创建独立的可描述的模块、程序片断以及待测试应用程序的脚本。这些小脚本进行组合&#xff0c;就能组成用来独立运行特定的测试的测试用例脚本。 场景一: 开发把 access_token接口地址由/cgi-bin/token 改为/cgi-bin/get_token或者修改参数等 》开发把…

Zigbee—基于Z-STACK组网

&#x1f3ac;慕斯主页&#xff1a;修仙—别有洞天 ♈️今日夜电波&#xff1a;チノカテ—ヨルシカ 0:46━━━━━━️&#x1f49f;──────── 4:08 &#x1f504; ◀️ ⏸ ▶️ ☰ &a…

Vue语音播报,不用安装任何包和插件,直接调用。

Vue语音播报功能可以通过使用浏览器提供的Web Speech API来实现。这个API允许你的应用程序通过浏览器朗读文本&#xff0c;不用安装任何包和插件&#xff0c;直接调用。以下是一个简单的介绍&#xff0c;演示如何在Vue中使用语音提示功能&#xff1a; 一、JS版本 <template…

基于springboot+vue的秒杀商城(前后端分离)

博主主页&#xff1a;猫头鹰源码 博主简介&#xff1a;Java领域优质创作者、CSDN博客专家、公司架构师、全网粉丝5万、专注Java技术领域和毕业设计项目实战 主要内容&#xff1a;毕业设计(Javaweb项目|小程序等)、简历模板、学习资料、面试题库、技术咨询 文末联系获取 项目介绍…

IntelliJ IDEA安装使用教程

IntelliJ IDEA是一个流行的Java 集成开发环境&#xff08;IDE&#xff09;&#xff0c;由JetBrains公司开发。它是一款全功能的IDE&#xff0c;支持多种编程语言&#xff0c;如Java、Kotlin、Groovy、Scala、Python、JavaScript、HTML、CSS等等。IntelliJ IDEA 提供了高效的代码…

SAP_ABAP_编程基础_列表_自定义列表 / 多页列表 / 列表页面设置

SAP ABAP 顾问&#xff08;开发工程师&#xff09;能力模型_Terry谈企业数字化的博客-CSDN博客文章浏览阅读494次。目标&#xff1a;基于对SAP abap 顾问能力模型的梳理&#xff0c;给一年左右经验的abaper 快速成长为三年经验提供超级燃料&#xff01;https://blog.csdn.net/j…

软件测试-测试用例案例及思维导图展示

自动售货机的测试用例 一个杯子的测试用例 一支笔的测试用例 朋友圈点赞的测试用例 功能测试 1点赞后是否显示结果 2.点赞后是否可以取消; 3.点赞取消后是否可以重复点赞; 4.共同好友点赞后&#xff0c;是否有消息提醒; 5.非共同好友点赞后&#xff0c;是否有消息提醒; 6.点击…

IDEA:官方汉化包

CtrlAlts进入setting找到Plugins&#xff0c;直接在如下的搜索框中输入chinese回车 之后就是这样的啦~

应用互斥:一次只能开启一个实例

在真实应用中&#xff0c;经常需要一个可执行文件&#xff0c;只能产生一个进程&#xff0c;如果多次执行可能导致bug。 最典型的应用是微信&#xff0c;它虽然不构成多个进程存在会报异常的问题。但是它是一个很好的例子。无论怎么操作都只能在一个环境下只有一个微信进程。 …

【矩阵论】Chapter 2—内积空间知识点总结复习

文章目录 内积空间1 内积空间2 标准正交向量集3 Gram-Schmidt正交化方法4 正交子空间5 最小二乘问题6 正交矩阵和酉矩阵 内积空间 1 内积空间 内积空间定义 设 V V V是在数域 F F F上的向量空间&#xff0c;则 V V V到 F F F的一个代数运算记为 ( α , β ) (\alpha,\beta) (α…

【GraphQL】PostGraphile简介

Introduction to PostGraphile 什么是PostGraphile&#xff1f; 如果您熟悉Spring Data JPA&#xff0c;那么理解PostGraphile将非常容易。但没关系。让我们来看看。PostgreSQL数据库是一个非常流行的高性能应用数据库。ProstGraphile与PostgreSQL数据库和GraphQL配合使用。 …

YOLOv5全网独家首发改进:SENetv2,Squeeze-Excitation模块融合Dense Layer,效果秒杀SENet

💡💡💡本文自研创新改进:SENet v2,针对SENet主要优化点,提出新颖的多分支Dense Layer,并与Squeeze-Excitation网络模块高效融合,融合增强了网络捕获通道模式和全局知识的能力 推荐指数:五星 收录 YOLOv5原创自研 https://blog.csdn.net/m0_63774211/catego…

安防监控系统的工作原理是什么?具体包含哪些组成部分?

关于安防监控系统&#xff0c;大家熟知的就是监控系统平台&#xff0c;其实不然&#xff0c;智能视频安防监控系统涵盖的内容非常多&#xff0c;今天小编就和大家一起来探讨一下。 安防监控视频系统主要分为以下7大类&#xff1a; 1、 摄像头采集图像 安防监控系统通常使用摄…

单片机实验(三)

前言 实验一&#xff1a;利用定时器T1的中断控制P1.7引脚输出音频信号&#xff0c;启动蜂鸣器发出一段熟悉的与众不同的具有10个音节的音乐音频。 实验二&#xff1a;使用定时器/计数器来实现一个LCD显示年、月、日、星期 、时、分、秒的电子表&#xff0c;要求时和分可以方便…

全系降3万,一把干到底,极越「智取」特斯拉

作者|德新 编辑|王博 11月30日&#xff0c;极越01官宣全系降价3万。 这意味着21.99万起步的极越01 Max&#xff0c;成为这个市场上入门门槛最低的带有城市智能驾驶辅助功能的车型。 要知道这是一台比Model Y大了一圈&#xff0c;全系配置了高阶智驾硬件&#xff0c;全系配高…

【Openstack Train安装】十二、Cinder安装

Cinder在块存储资源和计算服务&#xff08;Nova&#xff09;之间提供了一个抽象层。通过Cinder API&#xff0c;块存储可以被管理&#xff08;创建、销毁和分配等&#xff09;&#xff0c;而不需要知道提供存储的底层资源。 本文介绍Cinder安装步骤&#xff0c;Cinder需在控制节…

LeetCode(45)最长连续序列【哈希表】【中等】

目录 1.题目2.答案3.提交结果截图 链接&#xff1a; 最长连续序列 1.题目 给定一个未排序的整数数组 nums &#xff0c;找出数字连续的最长序列&#xff08;不要求序列元素在原数组中连续&#xff09;的长度。 请你设计并实现时间复杂度为 O(n) 的算法解决此问题。 示例 1&a…