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

相关推荐
码小猿的CPP工坊3 分钟前
C++弱引用智能指针std::weak_ptr使用介绍
开发语言·c++
sheji34169 分钟前
【开题答辩全过程】以 基于Java的智慧环卫垃圾收运管理系统设计与实现为例,包含答辩的问题和答案
java·开发语言
Flash.kkl11 分钟前
Linux——线程的同步和互斥
linux·开发语言·c++
sunfove14 分钟前
Python 面向对象编程:从过程式思维到对象模型
linux·开发语言·python
努力学习的小廉43 分钟前
【QT(七)】—— 常用控件(四)
开发语言·qt
CoderCodingNo1 小时前
【GESP】C++六级考试大纲知识点梳理, (3) 哈夫曼编码与格雷码
开发语言·数据结构·c++
froginwe111 小时前
C 标准库 - `<errno.h>`
开发语言
鹿角片ljp1 小时前
Java IO流案例:使用缓冲流恢复《出师表》文章顺序
java·开发语言·windows
纵有疾風起1 小时前
【Linux 系统开发】基础开发工具详解:自动化构建、版本控制与调试器开发实战
linux·服务器·开发语言·c++·经验分享·开源·bash
一只小bit1 小时前
Qt 文件:QFile 文件读写与管理教程
前端·c++·qt·gui