自己封装的一个简易的二维表类SimpleTable
在QT中,QTableWidget处理二维表格的功能很强大(QTableView更强大),但有时我们只想让它显示少量数据(文字和图片),这时,使用QTableWidget就有点不方便了(个人感觉)。
所以我对QTableWidget再做了一次封装(SimpleTable类),让它在处理小型表格时更方便。
代码很简单,要解释的就写在注释里面了,欢迎大家使用。
如果大家发现这个类的BUG的话,欢迎提出,大家共同学习。
上代码:
- //simpletable.h
- #ifndef SIMPLETABLE_H_
- #define SIMPLETABLE_H_
- #include <QtCore>
- #include <QtGui>
- class SimpleTable : public QTableWidget
- {
- Q_OBJECT
- private:
- public:
- //构造函数,无实际内容,直接调用的构造函数
- SimpleTable(QWidget *parent = 0) : QTableWidget(parent) { }
- SimpleTable(int row, int column, QWidget *parent = 0)
- : QTableWidget(row, column, parent) { }
- //设置某个单元格中的文字
- void SetCellText( int cx, int cy, const QString &text,
- int alignment = Qt::AlignLeft,
- const QIcon icon = QIcon() );
- //在某个单元格中放置一张小图片
- void SetCellPixmap(int cx, int cy, const QPixmap &pixmap,
- Qt::Alignment alignment = Qt::AlignCenter);
- //获取某个单元格中的字符串(如果没有,就返回一个空字符串)
- QString GetCellText(int cx, int cy);
- //获取某个单元格的图片(如果没有,就返回一张空图片)
- QPixmap GetCellPixmap(int cx, int cy);
- };
- #endif
- //simpletable.cpp
- #include "simpletable.h"
- void SimpleTable::SetCellText(int cx, int cy, const QString &text,
- int alignment, const QIcon icon)
- {
- //检查是否越界
- if( cx>=rowCount() || cy>=columnCount() )
- {
- qDebug() << "Fail, Out of Range";
- return;
- }
- //如果此单元格中已经有item了,就直接更改item中的内容
- //否则,新建一个item
- QTableWidgetItem *titem = item(cx, cy);
- if( NULL == titem )
- titem = new QTableWidgetItem;
- titem->setText(text);
- titem->setTextAlignment(alignment);
- //如果图标不为空,就为此item设置图标
- if( !icon.isNull() )
- titem->setIcon(icon);
- setItem(cx, cy, titem);
- }
- void SimpleTable::SetCellPixmap(int cx, int cy, const QPixmap &pixmap,
- Qt::Alignment alignment)
- {
- if( cx>=rowCount() || cy>=columnCount() )
- {
- qDebug() << "Fail, Out of Range";
- return;
- }
- //在item中设置图片有很多方法,但我还是觉得在其中放置带图片一个Label最简单
- QLabel *label = new QLabel(this);
- label->setAlignment(alignment);
- label->setPixmap(pixmap);
- setCellWidget(cx, cy, label);
- }
- QString SimpleTable::GetCellText(int cx, int cy)
- {
- QString result;
- if( cx>=rowCount() || cy>=columnCount() )
- {
- qDebug() << "Fail, Out of Range";
- return result;
- }
- QTableWidgetItem *titem = item(cx, cy);
- if( NULL != titem )
- {
- result = titem->text();
- }
- return result;
- }
- QPixmap SimpleTable::GetCellPixmap(int cx, int cy)
- {
- QPixmap result;
- if( cx>=rowCount() || cy>=columnCount() )
- {
- qDebug() << "Fail, Out of Range";
- return result;
- }
- QTableWidgetItem *titem = item(cx, cy);
- if( NULL == titem )
- return result;
- QLabel *label = dynamic_cast<QLabel*>( cellWidget(cx, cy) );
- result = *label->pixmap();
- return result;
- }
以下是一个简单的测试例子:
- //main.cpp
- //测试例子
- #include "simpletable.h"
- int main(int argc, char **argv)
- {
- QApplication app(argc, argv);
- SimpleTable table(20, 10);
- for(int i=0; i<20; i++)
- {
- for(int j=0; j<10; j++)
- {
- table.SetCellText(i, j, QString::number(i*10+j));
- }
- }
- table.show();
- return app.exec();
- }
http://blog.csdn.net/small_qch/article/details/7674733
自己封装的一个简易的二维表类SimpleTable的更多相关文章
- 写了一个字符串的二维表: TSta
STA 单元 (用到 System.SysUtils.TStringHelper): --------------------------------------------------------- ...
- Qt信号槽机制的实现(面试的感悟,猜测每一个类保存的一个信号和槽的二维表,实际使用函数指针 元对象 还有类型安全的检查设定等等)
因为面试时问了我这道题,导致我想去了解信号槽到底是如何实现的,于是贴着顺序看了下源码,大致了解了整个框架.网上关于信号槽的文章也很多,但是大部分都是将如何应用的,这里我就写一下我所理解的如何实现吧, ...
- 基于Dapper二次封装了一个易用的ORM工具类:SqlDapperUtil
基于Dapper二次封装了一个易用的ORM工具类:SqlDapperUtil,把日常能用到的各种CRUD都进行了简化封装,让普通程序员只需关注业务即可,因为非常简单,故直接贴源代码,大家若需使用可以直 ...
- VC6下OpenGL 开发环境的构建外加一个简单的二维网络棋盘绘制示例
一.安装GLUT 工具包 GLUT 不是OpenGL 所必须的,但它会给我们的学习带来一定的方便,推荐安装. Windows 环境下的GLUT 本地下载地址:glut-install.zip(大小约为 ...
- java_web学习(四) 二维表的制作(初步接触MVC)
我们需要做一个jsp页面,动态显示信息表的内容. 一.需求分析 1. 做一个实体类:StudentInfo (包含4个字段) 2. 如图模拟生成3条数据,本质上就是new StudentInfo ...
- Excel:一维表和二维表 互转
一.一维表转二维表 数据源: 一份流水账式的值班表,为了便于打印张贴,现在需要使其变成这样的样式: 也就是从一维表变成传说中的二维表. 1.新建查询 依次单击[数据]→[新建查询] →[从文件]→[从 ...
- Oracle【二维表管理:约束】
1.简单的表创建和字段类型最简单的方式去创建表(没有添加主键之类的约束条件)[Oracle的字段类型]number:数值类型--整数类型:number(a) 总长度a--小数类型:number(a,b ...
- 数据可视化之PowerQuery篇(四)二维表转一维表,看这篇文章就够了
https://zhuanlan.zhihu.com/p/69187094 数据分析的源数据应该是规范的,而规范的其中一个标准就是数据源应该是一维表,它会让之后的数据分析工作变得简单高效. 在之前的文 ...
- 议:如何将树形菜单形式的数据转化成HTML的二维表(相同内容需合并单元格)
一般做OA类管理系统,经常涉及到“组织架构”的概念,那么像这种有上下层级关系的数据一般会做成树形菜单的方式显示,底层代码必定会用到递归算法.这篇随笔的目的就是要谈谈除了用树形菜单来显示这种上下层级关系 ...
随机推荐
- WPF笔记(1.9 样式和控件模板)——Hello,WPF!
原文:WPF笔记(1.9 样式和控件模板)--Hello,WPF! 资源的另一个用途是样式设置: <Window > <Window.Resources> <St ...
- SqlServer IF Exists([database]|[table]|[prop]) / Column([Operation])
*************************** --判断数据库是否存在 IF EXISTS (SELECT * FROM MASTER..sysdatabases WHERE NAME = ' ...
- 开源欣赏wordpress之post.php
switch($action) { case 'postajaxpost': case 'post': case 'post-quickpress-publish': case 'post-quick ...
- Binary Tree Level Order Traversal 解答
Question Given a binary tree, return the level order traversal of its nodes' values. (ie, from left ...
- iOS 删除相册中照片--来自简书
来自:http://www.jianshu.com/p/ac18aa3f28c2 最近公司的app有一个新功能是在app中删除相册的照片 ,本来是一个比较简单地功能,在做的过程中却发现AssetsLi ...
- hdu 3478 Catch(染色 dfs 或 bfs )
Problem Description A thief is running away! We can consider the city to N–. The tricky thief starts ...
- WebService 通用接收方法
/** * @Title: getNetStatusRequest * @Description: TODO(2.1检查网络状态字符串) * @param: * @return: String * @ ...
- IOS拷贝文件到沙盒
- (void)copyFileFromResourceTOSandbox { //文件类型 NSString * docPath = [[NSBundle mainBundle] pathForRe ...
- Can you find it?(二分 二分+STL set map)
Can you find it? Time Limit : 10000/3000ms (Java/Other) Memory Limit : 32768/10000K (Java/Other) T ...
- ffmpeg学习笔记
对于每一个刚開始学习的人,刚開始接触ffmpeg时,想必会有三个问题最为关心,即ffmpeg是什么?能干什么?怎么開始学习?本人前段时间開始接触ffmpeg,在刚開始学习过程中.这三个问 ...