QT 多级表头的实现

QT 多级表头的实现

分享一个比较简单实用多级表头实现策略。
QTableWidget+QTableView结合。
两个表格,QTableWidget作为表头,QTableView作为表格内容

效果

多级表头

实现

两个表格,QTableWidget作为表头,QTableView作为表格内容。
表格比较长,设置了横向的滚动条。

表头

table_header 作为表头的QtableWidget

1
2
3
4
5
6
7
8
9
10
11
12
ui.table_header->horizontalHeader()->setVisible(false);//取消横向竖向表头
ui.table_header->verticalHeader()->setVisible(false);
ui.table_header->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);//取消横向竖向滚动条
ui.table_header->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
ui.table_header->setFrameShape(QFrame::NoFrame); //不接受绘制
ui.table_header->setFocusPolicy(Qt::NoFocus); //不接受焦点
ui.table_header->setEditTriggers(QAbstractItemView::NoEditTriggers); //不可编辑
ui.table_header->setSelectionMode(QAbstractItemView::NoSelection); //不可选择

ui.table_header->setRowCount(2);
ui.table_header->setColumnCount(30);
ui.table_header->setFixedHeight(ui.table_header->rowHeight(0) + ui.table_header->rowHeight(1)); //固定高度2行

有占两列的有占两行的
setSpan(0, 0, 2, 1); 参数:第一行第一列,占两行一列

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
   ui.table_header->setSpan(0, 0, 2, 1);
ui.table_header->setItem(0, 0, new QTableWidgetItem(QString::fromLocal8Bit("1号")));
ui.table_header->item(0, 0)->setTextAlignment(Qt::AlignCenter);

ui.table_header->setSpan(0, 1, 2, 1);
ui.table_header->setItem(0, 1, new QTableWidgetItem(QString::fromLocal8Bit("2号")));
ui.table_header->item(0, 1)->setTextAlignment(Qt::AlignCenter);

ui.table_header->setSpan(0, 2, 2, 2);
ui.table_header->setItem(0, 2, new QTableWidgetItem(QString::fromLocal8Bit("3时间")));
ui.table_header->item(0, 2)->setTextAlignment(Qt::AlignCenter);

ui.table_header->setSpan(0, 4, 2, 2);
ui.table_header->setItem(0, 4, new QTableWidgetItem(QString::fromLocal8Bit("4号")));
ui.table_header->item(0, 4)->setTextAlignment(Qt::AlignCenter);

ui.table_header->setSpan(0, 6, 2, 1);
ui.table_header->setItem(0, 6, new QTableWidgetItem(QString::fromLocal8Bit("5重量")));
ui.table_header->item(0, 6)->setTextAlignment(Qt::AlignCenter);

ui.table_header->setSpan(0, 7, 2, 1);
ui.table_header->setItem(0, 7, new QTableWidgetItem(QString::fromLocal8Bit("6宽度")));
ui.table_header->item(0, 7)->setTextAlignment(Qt::AlignCenter);

ui.table_header->setSpan(0, 8, 2, 1);
ui.table_header->setItem(0, 8, new QTableWidgetItem(QString::fromLocal8Bit("7径")));
ui.table_header->item(0, 8)->setTextAlignment(Qt::AlignCenter);

ui.table_header->setSpan(0, 9, 2, 1);
ui.table_header->setItem(0, 9, new QTableWidgetItem(QString::fromLocal8Bit("8时间")));
ui.table_header->item(0, 9)->setTextAlignment(Qt::AlignCenter);

ui.table_header->setSpan(0, 10, 1, 2);
ui.table_header->setItem(0, 10, new QTableWidgetItem(QString::fromLocal8Bit("9距离")));
ui.table_header->item(0, 10)->setTextAlignment(Qt::AlignCenter);
ui.table_header->setItem(1, 10, new QTableWidgetItem(QString::fromLocal8Bit("1卷")));
ui.table_header->item(1, 10)->setTextAlignment(Qt::AlignCenter);
ui.table_header->setItem(1, 11, new QTableWidgetItem(QString::fromLocal8Bit("2卷")));
ui.table_header->item(1, 11)->setTextAlignment(Qt::AlignCenter);

ui.table_header->setSpan(0, 12, 1, 2);
ui.table_header->setItem(0, 12, new QTableWidgetItem(QString::fromLocal8Bit("3位置")));
ui.table_header->item(0, 12)->setTextAlignment(Qt::AlignCenter);
ui.table_header->setItem(1, 12, new QTableWidgetItem(QString::fromLocal8Bit("4地址")));
ui.table_header->item(1, 12)->setTextAlignment(Qt::AlignCenter);
ui.table_header->setItem(1, 13, new QTableWidgetItem(QString::fromLocal8Bit("5地址")));
ui.table_header->item(1, 13)->setTextAlignment(Qt::AlignCenter);

ui.table_header->setSpan(0, 14, 1, 4);
ui.table_header->setItem(0, 14, new QTableWidgetItem(QString::fromLocal8Bit("6时间")));
ui.table_header->item(0, 14)->setTextAlignment(Qt::AlignCenter);
ui.table_header->setItem(1, 14, new QTableWidgetItem(QString::fromLocal8Bit("7卷")));
ui.table_header->item(1, 14)->setTextAlignment(Qt::AlignCenter);
ui.table_header->setItem(1, 15, new QTableWidgetItem(QString::fromLocal8Bit("8卷")));
ui.table_header->item(1, 15)->setTextAlignment(Qt::AlignCenter);
ui.table_header->setItem(1, 16, new QTableWidgetItem(QString::fromLocal8Bit("9")));
ui.table_header->item(1, 16)->setTextAlignment(Qt::AlignCenter);
ui.table_header->setItem(1, 17, new QTableWidgetItem(QString::fromLocal8Bit("10")));
ui.table_header->item(1, 17)->setTextAlignment(Qt::AlignCenter);

ui.table_header->setSpan(0, 18, 1, 6);
ui.table_header->setItem(0, 18, new QTableWidgetItem(QString::fromLocal8Bit("0总")));
ui.table_header->item(0, 18)->setTextAlignment(Qt::AlignCenter);
ui.table_header->setItem(1, 18, new QTableWidgetItem(QString::fromLocal8Bit("2")));
ui.table_header->item(1, 18)->setTextAlignment(Qt::AlignCenter);
ui.table_header->setItem(1, 19, new QTableWidgetItem(QString::fromLocal8Bit("3")));
ui.table_header->item(1, 19)->setTextAlignment(Qt::AlignCenter);
ui.table_header->setItem(1, 20, new QTableWidgetItem(QString::fromLocal8Bit("4")));
ui.table_header->item(1, 20)->setTextAlignment(Qt::AlignCenter);
ui.table_header->setItem(1, 21, new QTableWidgetItem(QString::fromLocal8Bit("5")));
ui.table_header->item(1, 21)->setTextAlignment(Qt::AlignCenter);
ui.table_header->setItem(1, 22, new QTableWidgetItem(QString::fromLocal8Bit("6")));
ui.table_header->item(1, 22)->setTextAlignment(Qt::AlignCenter);
ui.table_header->setItem(1, 23, new QTableWidgetItem(QString::fromLocal8Bit("7")));
ui.table_header->item(1, 23)->setTextAlignment(Qt::AlignCenter);

ui.table_header->setSpan(0, 24, 1, 6);
ui.table_header->setItem(0, 24, new QTableWidgetItem(QString::fromLocal8Bit("0总")));
ui.table_header->item(0, 24)->setTextAlignment(Qt::AlignCenter);
ui.table_header->setItem(1, 24, new QTableWidgetItem(QString::fromLocal8Bit("1")));
ui.table_header->item(1, 24)->setTextAlignment(Qt::AlignCenter);
ui.table_header->setItem(1, 25, new QTableWidgetItem(QString::fromLocal8Bit("2")));
ui.table_header->item(1, 25)->setTextAlignment(Qt::AlignCenter);
ui.table_header->setItem(1, 26, new QTableWidgetItem(QString::fromLocal8Bit("3")));
ui.table_header->item(1, 26)->setTextAlignment(Qt::AlignCenter);
ui.table_header->setItem(1, 27, new QTableWidgetItem(QString::fromLocal8Bit("4")));
ui.table_header->item(1, 27)->setTextAlignment(Qt::AlignCenter);
ui.table_header->setItem(1, 28, new QTableWidgetItem(QString::fromLocal8Bit("5")));
ui.table_header->item(1, 28)->setTextAlignment(Qt::AlignCenter);
ui.table_header->setItem(1, 29, new QTableWidgetItem(QString::fromLocal8Bit("6")));
ui.table_header->item(1, 29)->setTextAlignment(Qt::AlignCenter);

表格内容

table_content 即QTableView用来显示表格的内容

1
2
3
4
5
6
7
8
9
10
11
model = new QStandardItemModel;
ui.table_content->horizontalHeader()->setVisible(false);
ui.table_content->verticalHeader()->setVisible(false);
ui.table_content->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
ui.table_content->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
ui.table_content->setFrameShape(QFrame::NoFrame);
//ui.table_content->setFocusPolicy(Qt::NoFocus);
ui.table_content->setSelectionBehavior(QAbstractItemView::SelectRows);//选中行,而不是单元格
ui.table_content->setSelectionMode(QAbstractItemView::SingleSelection); //只可以单选
ui.table_content->setAlternatingRowColors(true);
ui.table_content->setEditTriggers(QAbstractItemView::NoEditTriggers);//不可编辑

内容就是从数据库中查询了

1
2
3
4
5
6
model->setItem(i, 0,  new QStandardItem(QString::fromUtf8(“内容1”))); 
model->setItem(i, 0, new QStandardItem(QString::fromUtf8(“内容2”)));
ui.table_content->setModel(model);
// 由于上面有个占据了两列,固定一列长度是100
ui.table_content->setColumnWidth(2, 200);
ui.table_content->setColumnWidth(3, 200);

设置滚动条

由于两个表格要同步设置,上面都把滚动条取消,这里统一设置。

1
2
3
4
5
6
7
8
9
//滚动条
verticalScrollBar = new QScrollBar(Qt::Vertical, this);
ui.table_content->setVerticalScrollBar(verticalScrollBar);
horizontalScrollBar = new QScrollBar(Qt::Horizontal, this);
ui.table_content->setHorizontalScrollBar(horizontalScrollBar);
ui.table_header->setHorizontalScrollBar(horizontalScrollBar);
//加到布局里。
ui.horizontalLayout->addWidget(verticalScrollBar);
ui.verticalLayout_5->addWidget(horizontalScrollBar);

我的布局:
滚动条布局
这样的布局为了放滚动条使用。

表格间隙

1
ui.verticalLayout_5->setSpacing(0); //无间隙

设置了此布局中无间隙。
这样模拟出来了一个表格。


QT 多级表头的实现
http://zanderchan666.github.io/2023/11/24/QT 多级表头/
作者
Zander
发布于
2023年11月24日
许可协议