转载:QT QTableView用法小结
出自:
http://blog.chinaunix.net/uid-20382483-id-3518513.html
QTableView常用于实现数据的表格显示。下面我们如何按步骤实现学生信息表格:
一 添加表头
//准备数据模型
QStandardItemModel *student_model = new QStandardItemModel();
student_model->setHorizontalHeaderItem(0, new QStandardItem(QObject::tr("Name")));
student_model->setHorizontalHeaderItem(1, new QStandardItem(QObject::tr("NO.")));
student_model->setHorizontalHeaderItem(2, new QStandardItem(QObject::tr("Sex")));
student_model->setHorizontalHeaderItem(3, new QStandardItem(QObject::tr("Age")));
student_model->setHorizontalHeaderItem(4, new QStandardItem(QObject::tr("College")));
//利用setModel()方法将数据模型与QTableView绑定
ui->student_tableview->setModel(student_model);
二 设置表格属性
//设置列宽不可变动,即不能通过鼠标拖动增加列宽
ui->student_tableview->horizontalHeader()->setResizeMode(0, QHeaderView::Fixed);
ui->student_tableview->horizontalHeader()->setResizeMode(1, QHeaderView::Fixed);
ui->student_tableview->horizontalHeader()->setResizeMode(2, QHeaderView::Fixed);
ui->student_tableview->horizontalHeader()->setResizeMode(3, QHeaderView::Fixed);
ui->student_tableview->horizontalHeader()->setResizeMode(4, QHeaderView::Fixed);
//设置表格的各列的宽度值
ui->student_tableview->setColumnWidth(0,100);
ui->student_tableview->setColumnWidth(1,100);
ui->student_tableview->setColumnWidth(2,100);
ui->student_tableview->setColumnWidth(3,100);
ui->student_tableview->setColumnWidth(4,100);
//默认显示行头,如果你觉得不美观的话,我们可以将隐藏
ui->student_tableview->verticalHeader()->hide();
//设置选中时为整行选中
ui->student_tableview->setSelectionBehavior(QAbstractItemView::SelectRows);
//设置表格的单元为只读属性,即不能编辑
ui->student_tableview->setEditTriggers(QAbstractItemView::NoEditTriggers);
//如果你用在QTableView中使用右键菜单,需启用该属性
ui->tstudent_tableview->setContextMenuPolicy(Qt::CustomContextMenu);
三 动态添加行
在表格中添加行时,我们只需要在model中插入数据即可,一旦model中的数据发生变化,QTabelView显示就会做相应的变动
//在第一行添加学生张三的个人信息(setItem函数的第一个参数表示行号,第二个表示列号,第三个为要显示的数据)
student_model->setItem(0, 0, new QStandardItem(“张三"));
student_model->setItem(0, 1, new QStandardItem("20120202"));
student_model->setItem(0, 2, new QStandardItem("男"));
student_model->setItem(0, 3, new QStandardItem("18"));
student_model->setItem(0, 4, new QStandardItem("土木学院"));
四 设置数据显示的样式
//设置单元格文本居中,张三的数据设置为居中显示
student_model->item(0, 0)->setTextAlignment(Qt::AlignCenter);
student_model->item(0, 1)->setTextAlignment(Qt::AlignCenter);
student_model->item(0, 2)->setTextAlignment(Qt::AlignCenter);
student_model->item(0, 3)->setTextAlignment(Qt::AlignCenter);
student_model->item(0, 4)->setTextAlignment(Qt::AlignCenter);
//设置单元格文本颜色,张三的数据设置为红色
student_model->item(0, 0)->setForeground(QBrush(QColor(255, 0, 0)));
student_model->item(0, 1)->setForeground(QBrush(QColor(255, 0, 0)));
student_model->item(0, 2)->setForeground(QBrush(QColor(255, 0, 0)));
student_model->item(0, 3)->setForeground(QBrush(QColor(255, 0, 0)));
student_model->item(0, 4)->setForeground(QBrush(QColor(255, 0, 0)));
//将字体加粗
student_model->item(0, 0)->setFont( QFont( "Times", 10, QFont::Black ) );
student_model->item(0, 1)->setFont( QFont( "Times", 10, QFont::Black ) );
student_model->item(0, 2)->setFont( QFont( "Times", 10, QFont::Black ) );
student_model->item(0, 3)->setFont( QFont( "Times", 10, QFont::Black ) );
student_model->item(0, 4)->setFont( QFont( "Times", 10, QFont::Black ) );
//设置排序方式,按年龄降序显示
student_model->sort(3, Qt::DescendingOrder);
①、
设置选中单个元素、整行、整列。
ui->student_tableview->setSelectionBehavior(QAbstractItemView::SelectRows);
enum QAbstractItemView::SelectionBehavior
Constant | Value | Description |
---|---|---|
QAbstractItemView::SelectItems |
0 |
Selecting single items. |
QAbstractItemView::SelectRows |
1 |
Selecting only rows. |
QAbstractItemView::SelectColumns |
2 |
Selecting only columns. |
②、设置是否允许多选:
ui->student_tableview->setSelectionMode(QAbstractItemView::SingleSelection);
enum QAbstractItemView::SelectionMode
This enum indicates how the view responds to user selections:
Constant | Value | Description |
---|---|---|
QAbstractItemView::SingleSelection |
1 |
When the user selects an item, any already-selected item becomes unselected, and the user cannot unselect the selected item by clicking on it. |
QAbstractItemView::ContiguousSelection |
4 |
When the user selects an item in the usual way, the selection is cleared and the new item selected. However, if the user presses the Shift key while clicking on an item, all items between the current item and the clicked item are selected or unselected, depending on the state of the clicked item. |
QAbstractItemView::ExtendedSelection |
3 |
When the user selects an item in the usual way, the selection is cleared and the new item selected. However, if the user presses the Ctrl key when clicking on an item, the clicked item gets toggled and all other items are left untouched. If the user presses the Shift key while clicking on an item, all items between the current item and the clicked item are selected or unselected, depending on the state of the clicked item. Multiple items can be selected by dragging the mouse over them. |
QAbstractItemView::MultiSelection |
2 |
When the user selects an item in the usual way, the selection status of that item is toggled and the other items are left alone. Multiple items can be toggled by dragging the mouse over them. |
QAbstractItemView::NoSelection |
0 |
Items cannot be selected. |
The most commonly used modes are SingleSelection and ExtendedSelection.
③、设置表的单元格的编辑模式
ui->student_tableview->setEditTriggers(QAbstractItemView.NoEditTriggers)
enum QAbstractItemView::EditTrigger
flags QAbstractItemView::EditTriggers
This enum describes actions which will initiate item editing.
Constant | Value | Description |
---|---|---|
QAbstractItemView::NoEditTriggers |
0 |
No editing possible. |
QAbstractItemView::CurrentChanged |
1 |
Editing start whenever current item changes. |
QAbstractItemView::DoubleClicked |
2 |
Editing starts when an item is double clicked. |
QAbstractItemView::SelectedClicked |
4 |
Editing starts when clicking on an already selected item. |
QAbstractItemView::EditKeyPressed |
8 |
Editing starts when the platform edit key has been pressed over an item. |
QAbstractItemView::AnyKeyPressed |
16 |
Editing starts when any key is pressed over an item. |
QAbstractItemView::AllEditTriggers |
31 |
Editing starts for all above actions. |
The EditTriggers type is a typedef for QFlags<EditTrigger>. It stores an OR combination of EditTrigger values
QTableView中嵌入组件:http://qimo601.iteye.com/blog/1538364
转载:QT QTableView用法小结的更多相关文章
- 【转载】QT QTableView用法小结
原始日期: 2016-08-16 09:28 来源:http://blog.csdn.net/wang_lichun/article/details/7805253 QTableView常用于实现数据 ...
- QT QTableView用法小结
QTableView常用于实现数据的表格显示.下面我们如何按步骤实现学生信息表格: 一 添加表头 //准备数据模型 QStandardItemModel *student_model = new QS ...
- QT QXmlStreamWriter用法小结
一 API介绍 writeStartDocument():写文档头,作用类似于创建一个xml文档,并在文档开头部分写入版本信息和编码信息,一般为: <?xml version="1.0 ...
- 转载:Hadoop排序工具用法小结
本文转载自Silhouette的文章,原文地址:http://www.dreamingfish123.info/?p=1102 Hadoop排序工具用法小结 发表于 2014 年 8 月 25 日 由 ...
- Java返回类型泛型的用法小结
Java返回类型泛型的用法小结 版权声明:本文为博主原创文章,未经博主允许不得转载. 关于Java泛型的基本用法就不多说了,主要是一个编译期的检查,也避免了我们代码中的强制转换,比较经典的用法有泛型D ...
- MVC图片上传详解 IIS (安装SSL证书后) 实现 HTTP 自动跳转到 HTTPS C#中Enum用法小结 表达式目录树 “村长”教你测试用例 引用provinces.js的三级联动
MVC图片上传详解 MVC图片上传--控制器方法 新建一个控制器命名为File,定义一个Img方法 [HttpPost]public ActionResult Img(HttpPostedFile ...
- [No000010]Ruby 中一些百分号(%)的用法小结
#Ruby 中一些百分号(%)的用法小结 #这篇文章主要介绍了Ruby 中一些百分号(%)的用法小结,需要的朋友可以参考下 what_frank_said = "Hello!"#% ...
- C++ typedef用法小结 (※不能不看※)
C++ typedef用法小结 (※不能不看※) 第一.四个用途 用途一: 定义一种类型的别名,而不只是简单的宏替换.可以用作同时声明指针型的多个对象.比如:char* pa, pb; // 这多数不 ...
- 函数fgets和fputs、fread和fwrite、fscanf和fprintf用法小结 (转)
函数fgets和fputs.fread和fwrite.fscanf和fprintf用法小结 字符串读写函数fgets和fputs 一.读字符串函数fgets函数的功能是从指定的文件中读一个字符串到字符 ...
随机推荐
- mysql格式化日期
mysql查询记录如果有时间戳字段时,查看结果不方便,不能即时看到时间戳代表的含义,现提供mysql格式换时间函数,可以方便的看到格式化后的时间. 1. DATE_FORMAT() 函数用于以不同的格 ...
- Oracle学习笔记之五,Oracle 11g的PL/SQL入门
1. PL/SQL概述 PL/SQL(Procedural Language/SQL)是Oracle的专用语言,是对标准SQL语言的扩展,它允许在其内部嵌套普通的SQL语句,还可以定义变量和常量,允许 ...
- OPENGL NEHE Lesson11 11课的计算公式推导
计算多边形公式推导: 条件x 离散的值从0到45; y离散的值从0到45; z是符合正弦波. 问题: 1 要求x’=f(x)映射到 x’ ∈[-4.5, 4.5], x ∈{0, 1, 2, …, 4 ...
- Avoid strong reference cycles
转自:http://masteringios.com/blog/2014/03/06/avoid-strong-reference-cycles/ With the introduction of A ...
- ntp服务的细节全解析
在linux里设置NTP服务并不难,但是NTP本身确是一个很复杂的协议. 你都了解细节么? 1. 时间和时区 date命令可显示时间与市区 [root@T_PV1_DB ~]# date Tue Fe ...
- Android App 内存泄露之调试工具(1)
Android App 内存泄露之工具(1) 使用内存监測工具 DDMS –> Heap 操作步骤 启动eclipse后,切换到DDMS透视图,并确认Devices视图.Heap视图都是打开的, ...
- 每日英语:Hong Kong Lifestyle Strains City's Resources
Hong Kong's rapacious consumption and waste production is straining its natural resources and could ...
- nyoj123 士兵杀敌(四)树状数组 插线问点
士兵杀敌(四) 时间限制:2000 ms | 内存限制:65535 KB 难度:5 描述 南将军麾下有百万精兵,现已知共有M个士兵,编号为1~M,每次有任务的时候,总会有一批编号连在一起人请战(编 ...
- asp.net mvc 5 在没有外网win2008R2服务器部署方法
我在本地用最新的.net 4.5和asp.net mvc 5框架做了一个小应用.本地都测试打包成功. 现在要放到服务器上,这个应用只是内网用.服务器不允许连接外网.看到www.asp.net 没有mv ...
- SharePoint自动化系列——通过Coded UI录制脚本自动化创建SharePoint Designer Reusable Workflow
Coded UI非常好,我开始还在想,怎么样能让一个通过SharePoint Designer创建的Workflow publish三百五十次?想不到一个好的方法,也不知道SharePoint Des ...