qt tableview代理实现

1. [图片] Screenshot-无标题窗口.png    


添加图片到资源文件夹



2. [文件] test.zip ~ 23KB     下载(647)     

3. [文件] main.cpp ~ 273B     下载(119)     

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/**
  * @file main.cpp
  * @brief 
  * @author xiangxw
  * @version 0.0
  * @date 2011年04月30日
  */
 
#include<QApplication>
#include"MyTableView.h"
 
 
int main( int argc, char *argv[])
{
     QApplication app(argc,argv);
 
     MyTableView table;
     table.show();
 
     return app.exec();
}

4. [文件] MyTableView.h ~ 1KB     下载(84)     

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/**
  * @file MyTableView.h
  * @brief
  * @author xiangxw
  * @version 0.0
  * @date 2011年04月30日
  */
 
#ifndef MYTABLEVIEW_H
#define MYTABLEVIEW_H
 
#include<QTableView>
#include<QItemDelegate>
#include<QStandardItemModel>
#include<QPixmap>
 
class MyTableView;
class MyItemDelegate;
class MyStandardItemModel;
 
class MyTableView: public QTableView
{
     public :
         MyTableView(QWidget * parent=0);
         virtual ~ MyTableView(){}
 
     protected :
         void mouseMoveEvent(QMouseEvent * event);
 
     private :
         MyItemDelegate * delegate;
         MyStandardItemModel * model;       
};
 
class MyItemDelegate: public QItemDelegate
{
     public :
         MyItemDelegate(QObject * parent=0);
         virtual ~ MyItemDelegate(){}
 
         void paint(QPainter * painter,
                 const QStyleOptionViewItem & option,
                 const QModelIndex & index) const ;
         bool editorEvent(QEvent * event,
                 QAbstractItemModel * model,
                 const QStyleOptionViewItem & option,
                 const QModelIndex & index);
     private :
         QPixmap favouritePixmap;
         QPixmap notFavouritePixmap;
 
};
 
class MyStandardItemModel: public QStandardItemModel
{
     public :
         MyStandardItemModel(QObject * parent=0)
             :QStandardItemModel(parent){}
         virtual ~ MyStandardItemModel(){}
         
         QVariant data( const QModelIndex & index,
                       int role=Qt::DisplayRole) const ;
         QVariant headerData( int section,
                 Qt::Orientation orientation,
                 int role=Qt::DisplayRole) const ;
};
 
#endif

5. [文件] MyTableView.cpp ~ 3KB     下载(107)     

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/**
  * @file MyTableView.cpp
  * @brief 
  * @author xiangxw
  * @version 0.0
  * @date 2011年04月30日
  */
 
#include<QPainter>
#include<QMouseEvent>
#include"MyTableView.h"
 
#ifndef NDEBUG
#include<QDebug>
#endif
 
MyTableView::MyTableView(QWidget * parent)
     :QTableView(parent)
{
     delegate= new MyItemDelegate;
     model= new MyStandardItemModel; 
     model->setRowCount(6);
     model->setColumnCount(8);
 
     this ->setModel(model);
     this ->setItemDelegate(delegate);
 
     this ->resizeColumnsToContents();
     this ->resizeRowsToContents();
     this ->setEditTriggers(QAbstractItemView::NoEditTriggers);
     this ->setSelectionBehavior(QAbstractItemView::SelectRows);
     this ->setMouseTracking( true ); //important
}
 
void MyTableView::mouseMoveEvent(QMouseEvent * event)
{
     int column= this ->columnAt(event->x());
     int row= this ->rowAt(event->y());
     if (column==0 && row!=-1){
         this ->setCursor(Qt::PointingHandCursor);
     }
     else {
         this ->setCursor(Qt::ArrowCursor);
     }
}
 
MyItemDelegate::MyItemDelegate(QObject * parent)
     :QItemDelegate(parent)
{
     favouritePixmap=QPixmap( ":/favourite.png" );
     notFavouritePixmap=QPixmap( ":/no-favourite.png" );
}
 
void MyItemDelegate::paint(QPainter * painter,
         const QStyleOptionViewItem & option,
         const QModelIndex & index) const
{
     if (index.column()!=0){
         QItemDelegate::paint(painter,option,index);
         return ;
     }
     const QAbstractItemModel * model=index.model();
     QVariant var=model->data(index,Qt::CheckStateRole);
     if (var.isNull()) var= false ;
     const QPixmap & star=var.toBool()?
                          favouritePixmap:notFavouritePixmap;
 
     int width=star.width();
     int height=star.height();
     QRect rect=option.rect;
     int x=rect.x()+rect.width()/2-width/2;
     int y=rect.y()+rect.height()/2-height/2;
 
     painter->drawPixmap(x,y,star);
}
 
bool MyItemDelegate::editorEvent(QEvent * event,
         QAbstractItemModel * model,
         const QStyleOptionViewItem & /*option*/ ,
         const QModelIndex & index)
{
     if (event->type()==QEvent::MouseButtonPress &&
             index.column()==0){
         QVariant var=model->data(index,Qt::CheckStateRole);
         bool isFavourite=var.toBool();
         if (var.isValid())
             isFavourite=isFavourite? false : true ;
         else
             isFavourite= true ;
         model->setData(index,isFavourite,Qt::CheckStateRole);
         return true ; //I have handled the event
     }
 
     return false ;
}
 
QVariant MyStandardItemModel::data(
         const QModelIndex & index,
         int role) const
{
     int column=index.column();
 
     if (role==Qt::DisplayRole && column!=0)
         return column+1;
     if (role==Qt::ToolTipRole && column==0)
         return tr( "love" );
     return QStandardItemModel::data(index,role);
}
 
QVariant MyStandardItemModel::headerData( int section,
         Qt::Orientation orientation,
         int role) const
{
     if (section==0 && orientation==Qt::Horizontal){
         if (role==Qt::DecorationRole)
             return QIcon( ":/favourite.png" );
         if (role==Qt::DisplayRole)
             return "" ;
         if (role==Qt::ToolTipRole)
             return tr( "love" );
     }
     return QStandardItemModel::headerData(section,orientation,role);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++ Qt5 范例开发大全 作者: Longki 目 录 第 1章 开发环境 1.1 Qt 简介。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 5 1.2 下载安装 Qt Creator 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。6 1.3 第一个程序 Hello World 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。7 第2章 窗体应用 1.1 窗体基类说明 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 12 1.2 控制窗体大小 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 13 1.3 窗体初始位置及背景色 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 13 1.4 修改标题栏图标 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 14 1.5 移动无边框窗体 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 16 1.6 去掉标题栏中最大化、最小化按钮 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 17 1.7 多窗体调用 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 18 1.8 字体形状窗体 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 20第 3章 控件应用 1.1 QPushButton 按钮 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 23 1.2 QLabel 标签 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 23 1.3 QLineEdit 单行文本 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 2 C++ Qt5 范例开发大全 24 1.4 QTextEdit 多行文本 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 25 1.5 QPlainTextEdit 多行文本 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 26 1.6 QComboBox 下拉列表框 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 26 1.7 QFontComboBox 字体下拉列表框 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 27 1.8 QSpinBox 控件 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 28 1.9 QTimeEdit 时间控件 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 29 1.10 QDateEdit 日期控件 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 30 1.11 QScrollBar 控件 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 30 1.12 QRadioButton 单选按钮 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 31 1.13 QCheckBox 复选框 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 32 1.14 QListView 列表控件 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 34 1.15 QTreeView 树控件 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 34 1.16 QTableView 表格控件 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 35 1.17 QHBoxLayout 横向布局 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 36 1.18 QGridLayout 网格布局 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 37 1.19 QGroupBox 控件 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 38 1.20 QTabWidget 控件 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 39 1.21 QMenu、 QToolBar 控件 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 41 1.22 任务栏托盘菜单 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 43 第 4章 组件应用 1.1 日历组件 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 47 1.2 登录窗口 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 48 3 C++ Qt5 范例开发大全 1.3 文件浏览对话框 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 50 1.4 颜色选择对话框 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 51 1.5 进度条实例。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 53 1.6Timer 实时更新时间 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 54 第 5章 文件操作 1.1 创建文件夹 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 57 1.2 写入文件 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 58 1.3 修改文件内容 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 60 1.4 删除文件 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 62 1.5 修改文件名 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 63 1.6 INI 文件写入操作 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 65 1.7 INI 文件读取操作 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 68 1.8 创建 XML 文件 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 71 1.9 读取 XML 文件 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 72 第 6章 图形图像操作 1.1 绘制文字 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 75 1.2 绘制线条 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 75 1.3 绘制椭圆 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 77 1.4 显示静态图像 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 78 1.5 显示动态图像 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 78 1.6 图片水平移动 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 79 1.7 图片翻转 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 80 1.8 图片缩放 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 82 4 C++ Qt5 范例开发大全 1.9 图片中加文字 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 84 1.10 图像扭曲 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 85 1.11 模糊效果 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 85 1.12 着色效果 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 86 1.13 阴影效果 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 87 1.14 透明效果 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 87 第 7章 多媒体应用 1.1 音频、视频播放器 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 90 1.2 播放 Flash 动画 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 94 1.3 播放图片动画 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 95 第 8章 系统操作 1.1 获取屏幕分辨率 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 98 1.2 获取本机名、 IP 地址 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 98 1.3 根据网址获取 IP 地址 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 99 1.4 判断键盘按下键值 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 100 1.5 获取系统环境变量 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 101 1.6 执行系统命令 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 102 第 9章 注册表 1.0 简要说明注册表 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 105 1.1 写入注册表 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 105 1.2 查找注册表 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 106 1.3 修改 IE 浏览器的默认主页 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 107 第 10章 数据库基础 1.1 查询数据库驱动 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 109 5 C++ Qt5 范例开发大全 1.2Qodbc 连接 Access 数据库 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 109 1.3 插入数据 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 111 1.4 数据列表 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 112 1.5 操作 SQLite 数据库 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 113 1.6SQLite 数据库视图管理器 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 115 第十一章 网络开发 1.1 点对点聊天服务端 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。119 1.2 点对点聊天客户端 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 123 1.3 局域网广播聊天 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 128 1.4SMTP 协议发送邮件 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 148 1.5 调用系统 DLL 判断网络连接状态 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 152 第十二章 进程与线程 1.1 进程管理器 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 155 1.2 线程 QThread 应用 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。158 1.3 线程 QRunnable 应用 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。159 第十三章 数据安全 1.1 QByteArray 加密数据 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 163 1.2 AES 加密数据 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 164 1.3 MD5 加密数据 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 165 1.4 生成随机数 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 166 第十四章 打包部署 1.1 FilePacker 打包 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。169 1.2 Inno Setup 打包 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 174

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值