-
Notifications
You must be signed in to change notification settings - Fork 0
/
GCListView.cpp
executable file
·147 lines (92 loc) · 3.05 KB
/
GCListView.cpp
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
//
// Created by gaochong on 17-11-24.
//
#include "GCListView.h"
GCListView::GCListView( QStringList TableHeader ) {
//整行选择
this->setSelectionBehavior( QAbstractItemView::SelectRows );
//不允许编辑
this->setEditTriggers( QAbstractItemView::NoEditTriggers );
//表头字体
//this->horizontalHeader()->setFont(QFont ("Simsun" , 12));
//设置行交替颜色
this->setAlternatingRowColors(true);
//只能单选一个项
//this->setSelectionMode(QAbstractItemView::SingleSelection);
//表头信息显示居左
//this->horizontalHeader()->setDefaultAlignment( Qt::AlignLeft );
//隐藏垂直表头
this->verticalHeader()->setVisible( true );
//隐藏行头
//this->verticalHeader()->hide();
//背景网格线设置
this->setShowGrid(false);
//网格背景画笔
this->setGridStyle( Qt::DotLine );
dataModel = new QStandardItemModel();
//设置标题
//m_dataModel->setHeaderData(0,Qt::Horizontal,"标题");
QStringList tbHeader = TableHeader;
//设置列数
dataModel->setColumnCount ( tbHeader.count() );
//设置表头
dataModel->setHorizontalHeaderLabels( tbHeader );
this->setModel( dataModel );
// 列宽自适应大小
//this->horizontalHeader()->setSectionResizeMode( QHeaderView::ResizeToContents );
//初始列宽定义
this->setColumnWidth( 0 ,200 );
this->setColumnWidth( 1 ,200 );
this->setColumnWidth( 2 ,250 );
this->setColumnWidth( 3 ,1000 );
//隐藏列
//this->setColumnHidden( 5,true );
//允许右键表格
this->setContextMenuPolicy( Qt::CustomContextMenu );
tableHead = this->horizontalHeader();
tableHead->setSortIndicator(0,Qt::AscendingOrder);
tableHead->setSortIndicatorShown(true);
connect( tableHead, SIGNAL(sectionClicked(int)), this, SLOT(view_sort(int)));
/**
//右键激活菜单
connect( this,
SIGNAL( customContextMenuRequested( QPoint )),
this,
SLOT( ShowContextMenu( QPoint )) );
*/
/**
connect( this,
SIGNAL(clicked(QModelIndex)),
this,
SLOT( Itemlick(QModelIndex) ));
connect( this,
SIGNAL(doubleClicked(QModelIndex)),
this,
SLOT( ItemDBClick(QModelIndex)));
**/
/**
* 初始化右键菜单
*/
//m_ContextMenu = new QMenu();
}
GCListView::~GCListView() {
delete dataModel;
}
void GCListView::ClearAllItem(){
dataModel->removeRows(0, dataModel->rowCount() );
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void GCListView::view_sort(int column) {
int i = tableHead->sortIndicatorOrder();
if( 0 == i ) {
dataModel->sort(column, Qt::AscendingOrder);
}else {
dataModel->sort(column, Qt::DescendingOrder);
}
}
QStandardItemModel* GCListView::getDataModel() {
return this->dataModel;
}
void GCListView::setDataModel(QStandardItemModel *DataModel) {
this->dataModel = DataModel;
}