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

相关推荐
张较瘦_11 分钟前
JavaScript | 数组方法实战教程:push()、forEach()、filter()、sort()
开发语言·javascript·ecmascript
Filotimo_24 分钟前
EntityGraph的概念
java·开发语言·数据库·oracle
wregjru25 分钟前
【读书笔记】Effective C++ 条款1~2 核心编程准则
java·开发语言·c++
lingran__1 小时前
C语言自定义类型详解 (1.1w字版)
c语言·开发语言
村口曹大爷1 小时前
JDK 24 正式发布:性能压轴,为下一代 LTS 铺平道路
java·开发语言
ysdysyn2 小时前
C# Modbus RTU 多从站控制全攻略:一端口,双轴控制
开发语言·c#·mvvm·通讯·modbus rtu
hashiqimiya2 小时前
java程序的并发
java·开发语言·python
微露清风2 小时前
系统性学习C++进阶-第十四讲-二叉搜索树
开发语言·c++·学习
董世昌413 小时前
强制类型转换和隐式类型转换的区别
开发语言
Fruiticecake3 小时前
Markdown,不用鼠标也能做笔记!
开发语言