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

相关推荐
代码中介商6 小时前
C++左值与右值:核心判断法则详解
开发语言·c++
JAVA9656 小时前
JAVA面试-并发篇 05-并发包AQS队列实现原理是什么
java·开发语言·面试
Halo_tjn6 小时前
反射与设计模式1
java·开发语言·算法
珊瑚里的鱼7 小时前
手撕单例模式中的饿汉模式和懒汉模式,懒汉模式还要再多加一个C++11版本的
开发语言·c++·单例模式
_不会dp不改名_7 小时前
python-opencv环境搭建
开发语言·python·opencv
HappyAcmen7 小时前
9.复盘API全套流程
开发语言·python
charlie1145141917 小时前
通用GUI编程技术——图形渲染实战(四十五)——D3D12资源与堆管理:从上传到驻留
开发语言·3d·图形渲染·win32
不会C语言的男孩7 小时前
C++ Primer 第12章:动态内存
开发语言·c++
踏着七彩祥云的小丑8 小时前
Go学习第1天:入门
开发语言·学习·golang·go
眠りたいです8 小时前
现代C++:C++17中的新库特性
开发语言·c++·c++20·c++17