QHeaderView

先零零碎碎的写些东西,最后再整理。

1、QHeaderView怎么来的

在QTableView等View类中,头部(可能是水平或垂直)都是由QHeaderView来管理的。例如,可以从QTableView获取两个HeaderView,分别管理列标和行标:

复制代码
    //调整列标
    QHeaderView* colHeaderView = ui->tableView->horizontalHeader();
    colHeaderView->setSectionsMovable(true);
//    colHeaderView->setSectionResizeMode(4,QHeaderView::Fixed); //备注列要限制宽度
//    colHeaderView->resizeSection(4,300);
    colHeaderView->moveSection(1,4);
    colHeaderView->setStretchLastSection(true);     //最后一列扩展

    //隐藏行标
    QHeaderView* rowHeaderView = ui->tableView->verticalHeader();
    rowHeaderView->setHidden(true);
复制代码

注意到上述代码中,我们通过ui->tableView->horizontalHeader();获取的是列标,而获取行标需要用ui->tableView->verticalHeader();

2、QHeaderView负责什么

对于Header,其实也是MVC结构的,也就是说,QHeaderView负责的是Header的布局,而Header显示什么内容是QXXModel负责的事情,如列标要如下设置:

复制代码
    QSqlTableModel* model = new QSqlTableModel(this);
    model->setTable("user");
    model->setSort(0,Qt::AscendingOrder);
    model->setHeaderData(0,Qt::Horizontal,_T("序号"));
    model->setHeaderData(1,Qt::Horizontal,_T("姓名"));
    model->setHeaderData(2,Qt::Horizontal,_T("工作单位"));
    model->setHeaderData(3,Qt::Horizontal,_T("联系电话"));
    model->setHeaderData(4,Qt::Horizontal,_T("备注"));
    model->select();

    ui->tableView->setModel(model);
复制代码

3、关于logicalIndex、VisualIndex

Table一般是多行多列,不管是水平头(列标)还是垂直头(行标),都是由section来代表某个行(列)的。举个例子,用下面代码,可以使列标能拖动换位:

tableView.horizontalHeader()->setMovable(true); 

而移动某一列后,你或许想通过捕获QHeaderView::sectionMoved 信号来做一些工作:

void QHeaderView::sectionMoved ( int logicalIndex, int oldVisualIndex, int newVisualIndex )   [signal]

This signal is emitted when a section is moved. The section's logical index is specified by logicalIndex, the old index by oldVisualIndex, and the new index position by newVisualIndex.

QHeaderView中,一个Section的logicalIndex是从Model中获取的, 与QModelIndex对应,是不随view列移动而移动的;而VisualIndex是在View中的重新排列(注意隐藏列也计入);

最后,若某些函数提到Section的position,与上述两个index完全是两回事,是指在tableView中的相对像素位置,不要混淆。

在QHeaderView中有很多在上述两个Index值间转换的函数

int QHeaderView::​visualIndex(int logicalIndex) const//根据logicalIndex获取visualIndex
int QHeaderView::​logicalIndex(int visualIndex) const //根据visualIndex获取logicalIndex

由于logicalIndex的含义相对固定,因此绝大多数操作是以logicalIndex为参数的:

复制代码
//某些列(行)标发生变化 [slot]
void QHeaderView::​headerDataChanged(Qt::Orientation orientation, int logicalFirst, int logicalLast);

//隐藏指定的列(行)标
void QHeaderView::​hideSection(int logicalIndex);

//指定的列(行)标是否隐藏
bool QHeaderView::​isSectionHidden(int logicalIndex) const

//指定某列的宽度(这个很有用的)
void QHeaderView::​resizeSection(int logicalIndex, int size)

//某些列(行)标被点击 [signal]
void QHeaderView::​sectionClicked(int logicalIndex)

//某些列(行)标被双击 [signal]
void QHeaderView::​sectionDoubleClicked(int logicalIndex)

//[signal] 某个section被左键按下的鼠标滑过
void QHeaderView::​sectionEntered(int logicalIndex)

//[signal] 某个section被按下
void QHeaderView::​sectionPressed(int logicalIndex)

//获取某个section的像素位置
int QHeaderView::​sectionPosition(int logicalIndex) const

//获取某个Section的尺寸控制方式
ResizeMode QHeaderView::​sectionResizeMode(int logicalIndex) const

//设置指定Section的尺寸方案
void QHeaderView::​setSectionResizeMode(int logicalIndex, ResizeMode mode)

//[signal] 某个Section的尺寸发生了变化
void QHeaderView::​sectionResized(int logicalIndex, int oldSize, int newSize)

//获取某个Section的尺寸
int QHeaderView::​sectionSize(int logicalIndex) const

//返回某个Section的尺寸建议值
int QHeaderView::​sectionSizeHint(int logicalIndex) const

//[slot] 新插入了一些Section
void QHeaderView::​sectionsInserted(const QModelIndex & parent, int logicalFirst, intlogicalLast)

//隐藏指定的Section
void QHeaderView::​setSectionHidden(int logicalIndex, bool hide)

//显示指定的Section
void QHeaderView::​showSection(int logicalIndex)

//设置Section的排序方案
void QHeaderView::​setSortIndicator(int logicalIndex, Qt::SortOrder order)
复制代码

而个别的也是以VisualIndex操作的. 一定记住无论Section的VisualIndex怎么变,但是LogicalIndex不会变!

//Moves the section at visual index from to occupy visual index to.
void QHeaderView::​moveSection(int from, int to)

//互换位置
void QHeaderView::​swapSections(int first, int second)

 

 

 
posted @   张疯牛  阅读(9256)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
石家庄坦图计算机科技有限公司 石家庄市丰收路220号泽润大厦17层
点击右上角即可分享
微信分享提示