QHeaderView的visualIndex和logicalIndex

You can identify a section using the logicalIndex() and logicalIndexAt() functions, or by its index position, using the visualIndex() and visualIndexAt() functions.


 The visual index will change if a section is moved, but the logical index will not change.

visualIndex是显示的index,如果用户拖动列头,那么该visualIndex会改变的。但是他的LogicalIndex是不会改变的。
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
QHeaderView是Qt中的一个类,用于显示表格或列表的列名或行号。重写paintSection和paintEvent可以自定义QHeaderView的外观和行为。 paintSection函数用于绘制每个section(即列或行)。可以通过在paintSection函数中重新实现QStylePainter的drawControl函数来自定义section的外观。例如,以下示例将section的背景颜色设置为红色: ```cpp void MyHeaderView::paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const { QStyleOptionHeader option; initStyleOption(&option, logicalIndex); option.rect = rect; // Set the background color to red option.palette.setColor(QPalette::Background, Qt::red); QStylePainter stylePainter(painter->device()); stylePainter.drawControl(QStyle::CE_Header, option); } ``` paintEvent函数用于绘制整个QHeaderView。可以通过在paintEvent函数中调用paintSection函数来绘制每个section。例如,以下示例将所有section的背景颜色设置为蓝色: ```cpp void MyHeaderView::paintEvent(QPaintEvent *event) { QStyleOptionHeader option; initStyleOption(&option); QPainter painter(this); painter.fillRect(event->rect(), Qt::blue); for (int i = 0; i < count(); ++i) { QRect sectionRect = visualRect(i); if (event->rect().intersects(sectionRect)) { paintSection(&painter, sectionRect, i); } } } ``` 需要注意的是,在重写paintSection函数和paintEvent函数时,应该调用initStyleOption函数来初始化QStyleOptionHeader结构体,以便正确获取section的样式信息。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值