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 );// 代表正序

相关推荐
光影少年9 分钟前
RN 架构:JS层、原生桥接层、原生渲染层 通信原理
开发语言·javascript·架构
ALex_zry25 分钟前
C++26 std::inplace_vector 详解:零堆分配的定容向量
开发语言·c++
许彰午38 分钟前
77_Python数据清洗实战技巧
开发语言·python
落寞的电源42 分钟前
Delegate = Object + MethodInfo
开发语言·数据库·c#
你怎么知道我是队长1 小时前
JavaScript的介绍
开发语言·javascript·ecmascript
江华森1 小时前
04-python-面向对象
开发语言·python
AI行业学习1 小时前
Notepad++ 官方纯净下载+完整安装教程(Windows)【2026.7.5】
开发语言·windows·python·前端框架·html·notepad++
前端百草阁1 小时前
JavaScript 设计模式(23 种)
开发语言·前端·javascript·设计模式
EntyIU1 小时前
Java NIO 实战
java·开发语言·nio
浩瀚之水_csdn2 小时前
Python 3 网络编程详解:从原理到实战
开发语言·网络·python