Qt应用开发(基础篇)——头部视图 QHeaderView

一、前言

        QHeaderView类继承于QAbstractItemView,为项目视图(QTableViewQTreeView等)提供标题行或标题列。

        树结构视图 QTreeView

        表格视图 QTableView

        视图基类 QAbstractItemView

        QHeaderViewsection的概念,表示整条标题栏的一个个小部分,但是叫“部分”语句经常不通顺,所以下文都称呼为“项”。

        QHeaderView显示视图所需要的标题栏,例如QTableViewQTreeView类,本身它也是Model/View框架的一部分。视图中要设置头部数据可以使用以下三种方式。

model->setHorizontalHeaderLabels(QStringList()<<"title”);
model->setHeaderData(0,Qt::Horizontal,"title");
model->setHorizontalHeaderItem(0,new QStandardItem("title"));

        QHeaderView每一项都有自己的水平对齐orientation和项索引序号sections number,通过count()函数可以知道标题头的项个数,索引从0开始。使用resizeSection()moveSection()hideSection()、showSection()对节进行调整、移动、隐藏和显示。

ui->tableView->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Stretch)

        QHeaderView每一项可以是固定的,也可以使用setSectionsMovable()设置为可移动。如果需要手动调整每项的大小,需要使用setSectionsClickable()设置为可点击并setSectionResizeMode()设置调整大小的行为。如果用户移动了一个项,会触发sectionMoved()信号,如果用户调整了一项的大小,则会触发sectionResized()信号,而当鼠标点击时,则会触发sectionClicked()信号和sectionhandledoubleclick()信号。头文件也会发出sectionCountChanged()信号。

二、QHeaderView

1、属性

1)cascadingSectionResizes

        该属性表示当用户调整大小的项达到最小大小时,是否将调整大小级联到下一项。该属性仅当ResizeMode设置为Interactive生效。默认值为false。

bool cascadingSectionResizes() const
void setCascadingSectionResizes(bool enable)
2)defaultAlignment

        该属性表示标题头文本的对齐方式。

Qt::Alignment defaultAlignment() const
void setDefaultAlignment(Qt::Alignment alignment)
3)defaultSectionSize

        该属性表示头每项在调整之前的默认大小,该属性只有当ResizeMode设置为Interactive或者Fixed生效。

int defaultSectionSize() const
void setDefaultSectionSize(int size)
void resetDefaultSectionSize()
4)firstSectionMovable

        该属性表示第一项是否可以被用户移动。在QTreeView中,第一项保存树结构,因此默认情况下是不可移动的,即使在setSectionsMovable(true)之后也是如此。要让此方法生效,需使用了setSectionsMovable(true)。

bool isFirstSectionMovable() const
void setFirstSectionMovable(bool movable)
5)highlightSections

        该属性表示是否头部项要跟随视图(tableview\treeview)选中的项一起高亮。

bool highlightSections() const
void setHighlightSections(bool highlight)
6)maximumSectionSize

        该属性表示项的最大尺寸。

int maximumSectionSize() const
void setMaximumSectionSize(int size)
7)minimumSectionSize

        该属性表示项的最小尺寸。

int minimumSectionSize() const
void setMinimumSectionSize(int size)
8)showSortIndicator

        该属性表示是否显示排序指示。

bool isSortIndicatorShown() const
void setSortIndicatorShown(bool show)
9)stretchLastSection

        该属性表示确定头文件中最后一个可见项是否占用了所有可用空间,默认值为false。

bool stretchLastSection() const
void setStretchLastSection(bool stretch)

2、公共方法

1)count

        返回项的数量。

int count() const
2)hiddenSectionCount

        返回被隐藏的项个数。

int hiddenSectionCount() const
3)isSectionHidden

        返回索引下的项是否被隐藏。

bool isSectionHidden(int logicalIndex) const
4)setSectionHidden

       设置索引下的项被隐藏或者显示。

void setSectionHidden(int logicalIndex, bool hide)
5)hideSection

       隐藏索引下的项。

void hideSection(int logicalIndex)
6)showSection

        显示索引下的项。

void showSection(int logicalIndex)
7)length

        返回标题方向的长度。

int length() const
8)offset

        返回标头的偏移量,这是标头的最左(或垂直标头的最上)可见像素。

int offset() const
9)setOffset

        设置标头的偏移量。

void setOffset(int offset)
10)resizeContentsPrecision

        表头分区大小使用的精度,精度是指表头分区调整大小时可以接受的最小增量。

int resizeContentsPrecision() const
11)setResizeContentsPrecision

        设置调整表头分区大小时使用的精度,较低的值将提供不太准确但快速的自动调整大小,而较高的值将提供更准确的调整大小,但可能很慢。

        默认为1000,表示具有自动调整大小的水平列在执行自动调整大小时将在计算时查看最多1000行。特殊值0表示它将只查看可见区域。特殊值-1表示查看所有元素。

void setResizeContentsPrecision(int precision)
12)sectionResizeMode

        返回索引下项的调整大小策略。

QHeaderView::ResizeMode sectionResizeMode(int logicalIndex) const
13)setSectionResizeMode

        设置项的调整大小策略。

void setSectionResizeMode(QHeaderView::ResizeMode mode)
QHeaderView::ResizeMode描述
Interactive用户可以调整部分的大小。也可以使用resizeSection()调整该节的大小。
Fixed用户无法调整节的大小。只能使用resizeSection()调整节的大小。
StretchQHeaderView将自动调整部分大小以填充可用空间。该大小不能由用户或以编程方式更改。
ResizeToContentsQHeaderView将根据整个列或行的内容自动调整节的大小到最佳大小。该大小不能由用户或以编程方式更改。

14)resizeSection

        将logicalIndex指定的项大小调整为以像素为单位测量的大小。size参数必须大于等于零。size可以设置为0,不建议这么做,因为可以使用hideSection()隐藏。

void resizeSection(int logicalIndex, int size)
15)restoreState

        恢复视图的状态,如果状态被恢复,这个函数返回true,否则返回false。

bool restoreState(const QByteArray &state)
16)saveState

        保存视图的状态。

QByteArray saveState() const
17)sectionPosition

        返回逻辑索引下的节位置。

int sectionPosition(int logicalIndex) const
18)sectionSize

        返回逻辑索引下的节的宽度或者高度。

int sectionSize(int logicalIndex) const
19)sectionSizeHint

        用于检索表头分区的大小提示。它可以用来获取表头分区的大小提示,即表头分区的期望大小(相对于表头内容而言)。

int sectionSizeHint(int logicalIndex) const
20)sectionViewportPosition

        返回给定logicalIndex的项视图位置。

int sectionViewportPosition(int logicalIndex) const
21)sectionsClickable

        返回项是否可点击。

bool sectionsClickable() const
22)setSectionsClickable

        设置项是否可点击

void setSectionsClickable(bool clickable)
23)sectionsMovable

        返回项是否可移动。

bool sectionsMovable() const
24)setSectionsMovable

        设置项是否可移动。

void setSectionsMovable(bool movable)
25)sortIndicatorOrder

        返回排序规则。

Qt::SortOrder sortIndicatorOrder() const
26)setSortIndicator

        设置排序规则,正序Qt::AscendingOrder和倒序Qt::DescendingOrder。

void setSortIndicator(int logicalIndex, Qt::SortOrder order)
27)stretchSectionCount

        返回设置为调整模式拉伸QHeaderView::Stretch的项数。

int stretchSectionCount() const
28)visualIndex

        返回由给定的logicalIndex指定的项的可视索引位置。

int visualIndex(int logicalIndex) const
29)visualIndexAt

        返回覆盖视窗中给定位置position的项的可视索引。

int visualIndexAt(int position) const
30)logicalIndex

        返回给定visualIndex位置的部分的logicalIndex。

int logicalIndex(int visualIndex) const
31)logicalIndexAt

        返回条件下的索引。

int logicalIndexAt(int position) const
int logicalIndexAt(int x, int y) const
int logicalIndexAt(const QPoint &pos) const

3、信号

1)geometriesChanged

        当头部的几何形状发生变化时触发该信号。

void geometriesChanged()
2)sectionClicked

        单击某项时触发该信号。

void sectionClicked(int logicalIndex)
3)sectionCountChanged

        当项的数量发生变化时,即当项被添加或删除时触发该信号。

void sectionCountChanged(int oldCount, int newCount)
4)sectionDoubleClicked

        当双击某项时触发该信号。

void sectionDoubleClicked(int logicalIndex)
5)sectionEntered

        当光标移动到某项上并且按下鼠标左键时触发该信号。

void sectionEntered(int logicalIndex)
6)sectionHandleDoubleClicked

        当双击某项时触发该信号。

void sectionHandleDoubleClicked(int logicalIndex)
7)sectionMoved

        这个信号是当某项被移动时触发该信号。

void sectionMoved(int logicalIndex, int oldVisualIndex, int newVisualIndex)
8)sectionPressed

       当按下某项时触发该信号。

void sectionPressed(int logicalIndex)
9)sectionResized

       当某项重新设置大小的时候触发该信号。

void sectionResized(int logicalIndex, int oldSize, int newSize)
10)sortIndicatorChanged

       当某项重新设置排序的时候触发该信号。

void sortIndicatorChanged(int logicalIndex, Qt::SortOrder order)
  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
QHeaderView 是一个用于显示表格头部信息的控件,它支持多层表头。如果需要实现多层表头,可以使用 QStandardItemModel 来创建一个有层次结构的表格模型,然后将这个模型设置给 QTableView 控件。然后,QHeaderView 控件就可以根据这个模型来显示多层表头。 下面是一个简单的示例代码,演示如何使用 QStandardItemModel 和 QTableView 实现多层表头: ```cpp QStandardItemModel *model = new QStandardItemModel(); // 设置列数和行数 model->setColumnCount(3); model->setRowCount(2); // 设置第一层表头 model->setHeaderData(0, Qt::Horizontal, "A"); model->setHeaderData(1, Qt::Horizontal, "B"); model->setHeaderData(2, Qt::Horizontal, "C"); // 设置第二层表头 QList<QStandardItem*> secondHeaders; for (int i = 0; i < 3; i++) { QStandardItem *item = new QStandardItem(); item->setData("Sub" + QString::number(i), Qt::DisplayRole); secondHeaders.append(item); } model->insertRow(0, secondHeaders); QTableView *tableView = new QTableView(); tableView->setModel(model); // 设置表头控件 QHeaderView *headerView = new QHeaderView(Qt::Horizontal, tableView); headerView->setSectionsClickable(true); headerView->setSectionResizeMode(QHeaderView::Stretch); tableView->setHorizontalHeader(headerView); // 显示表格 tableView->show(); ``` 在这个示例中,我们首先创建了一个 QStandardItemModel 对象,并设置了两层表头。第一层表头包含了三个列,显示为 "A"、"B" 和 "C"。第二层表头包含了三个子列,显示为 "Sub0"、"Sub1" 和 "Sub2"。 然后,我们创建了一个 QTableView 控件,并将我们创建的模型设置给它。接着,我们创建了一个 QHeaderView 控件,并将它设置为水平表头。最后,我们将这个表头控件设置给 QTableView 控件,然后显示表格。 这样,我们就实现了一个带有多层表头的表格。如果需要更复杂的表头结构,可以继续使用 QStandardItemModel 来创建更深层次的表格模型。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

波塞冬~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值