qt QTableview 左侧 序号 倒序

本文主要在QTableview插入数据的基础上,使左边序号实现倒序,实现如下图所示。

解决办法:

QTableview左侧是QHeaderView类构成的, 重写QHeaderView的paintSection, 重写序号的文字内容,进而****实现QTableview 左侧序号倒序

定义新类,并调用

复制代码
//.h 
class RealTimeTableHeaderView : public QHeaderView
{
public:
      NewHeaderView(Qt::Orientation orientation,
            QWidget * parent = nullptr) :
            QHeaderView(orientation, parent)
        {
            ;
        }

protected:
    void paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const  override;
};


//.cpp

 void NewHeaderView::paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const
 {
     QHeaderView::paintSection(painter, rect, count() - logicalIndex - 1);
 }

调用:

复制代码
    NewHeaderView *m_pNewHeaderView = nullptr;
    m_pNewHeaderView = new NewHeaderView(Qt::Vertical, ui->show_realtime_tableView);
    ui->tableView->setVerticalHeader(m_pNewHeaderView);

主要是:

QHeaderView::paintSection(painter, rect, count() - logicalIndex - 1);// 代表倒序

QHeaderView::paintSection(painter, rect, logicalIndex );// 代表正序

相关推荐
2601_9620715717 小时前
【Java报错已解决】org.springframework.beans.factory.BeanCreationException
java·开发语言
淡海水18 小时前
07-04-并发-ConcurrentBag-T-工作窃取WorkStealing算法
开发语言·算法·c#·bag·concurrent·workstealing
CS_Zero19 小时前
C语言sizeof是函数吗?
c语言·开发语言
-今昭-20 小时前
《V2V 迁移实战:将 VMware 虚拟机转为 OpenStack 可用 Glance qcow2 镜像》
开发语言·python
黑马程序员毕设1 天前
基于Java的医院药品管理系统的优化设计与实现
java·开发语言·spring boot·小程序·架构·课程设计·毕设
_Twink1e1 天前
openJiuwen 实训营 Day 1 · WorkSwarm 入门教程
开发语言·c++·openjiuwen
问天_观心1 天前
大模型微调学习(一)
开发语言·人工智能·学习·语言模型·github
阿里嘎多学长1 天前
2026-09-03 GitHub 热点项目精选
开发语言·程序员·github·代码托管
大丑哥1 天前
QT QML 中实现高亮功能
qt
源代码•宸1 天前
前置准备:定时微服务背景和现状
开发语言·经验分享·后端·微服务·云原生·架构·golang