Qt中的 tableView 设置 二进制 十六进制 序号表头

2024-04-25 00:12

本文主要是介绍Qt中的 tableView 设置 二进制 十六进制 序号表头,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

二 进制序号

在这里插入图片描述

因为QTableView的垂直表头并不支持使用委托来自定义。
相反,可以通过将自定义的QWidget作为QHeaderView的标签来实现这一目标。

代码:

#include <QApplication>
#include <QMainWindow>
#include <QVBoxLayout>
#include <QScrollArea>
#include <QTableView>
#include <QStandardItemModel>
#include <QHeaderView>
#include <QLabel>
#include <QPainter>class BinaryHeaderView : public QHeaderView {
public:BinaryHeaderView(Qt::Orientation orientation, QWidget *parent = nullptr) : QHeaderView(orientation, parent) {}protected:void paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const override {if (orientation() == Qt::Vertical) {QString binary = QString::number(logicalIndex, 2).rightJustified(4, '0');painter->save();painter->drawText(rect, Qt::AlignCenter, binary);painter->restore();} else {QHeaderView::paintSection(painter, rect, logicalIndex);}}
};//------------------------------------------------------------------------以上复制到自己代码int main(int argc, char *argv[]) {QApplication app(argc, argv);QMainWindow mainWindow;// 创建表格QTableView tableView;// 创建模型和数据QStandardItemModel model(10, 10);for (int row = 0; row < 10; ++row) {for (int column = 0; column < 10; ++column) {QModelIndex index = model.index(row, column, QModelIndex());model.setData(index, QVariant(row * 10 + column + 1));}}tableView.setModel(&model);// 设置水平表头tableView.horizontalHeader()->hide();//------------------------------------------------------------------------以下复制到自己代码// 创建垂直表头BinaryHeaderView *verticalHeader = new BinaryHeaderView(Qt::Vertical, &tableView);tableView.setVerticalHeader(verticalHeader);
//------------------------------------------------------------------------end// 将表格添加到主窗口mainWindow.setCentralWidget(&tableView);mainWindow.resize(600, 400);mainWindow.show();return app.exec();
}

在这个示例中,自定义了BinaryHeaderView类,继承自QHeaderView,
重写了paintSection方法来绘制二进制序列。然后,将这个自定义的垂直表头应用到了QTableView中。

十六 进制序号

在这里插入图片描述

#include <QApplication>
#include <QMainWindow>
#include <QVBoxLayout>
#include <QScrollArea>
#include <QTableView>
#include <QStandardItemModel>
#include <QHeaderView>
#include <QPainter>class HexHeaderView : public QHeaderView {
public:HexHeaderView(Qt::Orientation orientation, QWidget *parent = nullptr) : QHeaderView(orientation, parent) {}protected:void paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const override {if (orientation() == Qt::Vertical) {QString hex = QString("%1").arg(logicalIndex * 16, 4, 16, QChar('0'));painter->save();painter->drawText(rect, Qt::AlignCenter, hex);painter->restore();} else {QHeaderView::paintSection(painter, rect, logicalIndex);}}
};int main(int argc, char *argv[]) {QApplication app(argc, argv);QMainWindow mainWindow;// 创建表格QTableView tableView;// 创建模型和数据QStandardItemModel model(10, 10);for (int row = 0; row < 10; ++row) {for (int column = 0; column < 10; ++column) {QModelIndex index = model.index(row, column, QModelIndex());model.setData(index, QVariant(row * 10 + column + 1));}}tableView.setModel(&model);// 设置水平表头tableView.horizontalHeader()->hide();// 创建垂直表头HexHeaderView *verticalHeader = new HexHeaderView(Qt::Vertical, &tableView);tableView.setVerticalHeader(verticalHeader);// 将表格添加到主窗口mainWindow.setCentralWidget(&tableView);mainWindow.resize(600, 400);mainWindow.show();return app.exec();
}

这篇关于Qt中的 tableView 设置 二进制 十六进制 序号表头的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/933223

相关文章

[word] word设置上标快捷键 #学习方法#其他#媒体

word设置上标快捷键 办公中,少不了使用word,这个是大家必备的软件,今天给大家分享word设置上标快捷键,希望在办公中能帮到您! 1、添加上标 在录入一些公式,或者是化学产品时,需要添加上标内容,按下快捷键Ctrl+shift++就能将需要的内容设置为上标符号。 word设置上标快捷键的方法就是以上内容了,需要的小伙伴都可以试一试呢!

二进制文件转化成文本文件

文章中如果有写错、表述不明、有疑问或者需要扩展的知识,欢迎留言或者私信~   1.区别 如果一个文件说是文本文件,使用任何一种文本编辑器打开可以展现出人类可读信息字符,因为编码都符合某种编码方式,如ASCII、UTF8、GB2312等等(在文件头可以读出来是什么编码方式,然后文本编辑器再按照规则去读取翻译成对应的字符,展示给我们的就是可读的了)。(关于编码方式不了解可以看这一篇) 如果一

JAVA读取MongoDB中的二进制图片并显示在页面上

1:Jsp页面: <td><img src="${ctx}/mongoImg/show"></td> 2:xml配置: <?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001

LeetCode--171 Excel表列序号

题目 给定一个Excel表格中的列名称,返回其相应的列序号。例如,A -> 1B -> 2C -> 3...Z -> 26AA -> 27AB -> 28 ... 示例 示例 1:输入: "A"输出: 1示例 2:输入: "AB"输出: 28示例 3:输入: "ZY"输出: 701 class Solution {public:int titleToNumber(strin

如何设置windows计划任务

如何设置windows计划任务 前言:在工作过程中写了一个python脚本,用于调用jira接口查询bug单数量,想要在本地定时任务执行,每天发送到钉钉群提醒,写下操作步骤用于记录。 1. 准备 Python 脚本 确保你的 Python 脚本已经保存到一个文件,比如 jira_reminder.py。 2. 创建批处理文件 为了方便任务计划程序运行 Python 脚本,创建一个批处理文

FastAdmin/bootstrapTable 表格中生成的按钮设置成文字

公司有个系统后台框架用的是FastAdmin,后台表格的操作栏按钮只有图标,想要设置成文字。 查资料后发现其实很简单,主需要新增“text”属性即可,如下 buttons: [{name: 'acceptcompany',title: '复核企业',text:'复核企业',classname: 'btn btn-xs btn-primary btn-dialog',icon: 'fa fa-pe

众所周知,配置即代码≠基础设置即代码

​前段时间翻到几条留言,问: “配置即代码和基础设施即代码一样吗?” “配置即代码是什么?怎么都是基础设施即代码?” 我们都是知道,DevOp的快速发展,让服务器管理与配置的时间大大减少,配置即代码和基础设施即代码作为DevOps的重要实践,在其中起到了关键性作用。 不少人将二者看作是一件事,配置即大代码是关于管理特定的应用程序配置设置本身,而基础设施即代码更关注的是部署支持应用程序环境所需的

QT 中ListView和ListWidget有什么区别

ListView和ListWidget在Qt框架中都是用于显示列表数据的控件,但它们在使用方法和特性上存在一些明显的差异。以下是关于它们用法不一样的地方的详细分析: 数据管理方式: ListView:使用QAbstractItemModel数据模型来管理和显示列表数据。QAbstractItemModel是一个抽象类,允许开发者自定义数据模型以适应特定的数据结构和需求。这使得ListView在处

设置Nginx缓存策略

详细信息 Nginx服务器的缓存策略设置方法有两种:add_header或者expires。 1. add_header 1)语法:add_header name value。 2)默认值:none。 3)使用范围:http、server、location。 配置示例如下: add_header cache-control "max-age=86400";#设置缓存时间为1天。add

设置android返回键,保存和取得最高分

1.在.h中声明一些方法 virtual void keyBackClicked();           //Android返回键 bool isHaveSaveFile(); void getHighestHistoryScore(); 在.cpp中实现这个几个方法 void WelcomeLayer::keyBackClicked(