qt table 简易封装,样式美化,以及 合并表格和颜色的区分 已解决

在需求中, 难免会使用 table 进行渲染窗口,做一个简单的封装。美化表格最终效果!!!

        

代码部分

	// 显示 20行 20列CCendDetailsInfoTableWidget* table = new CCendDetailsInfoTableWidget(20,10);for (int i = 0; i < 20; i++){for (int j = 0; j < 10; j++){table->setCellText(i, j,QString::number(i * 10 + j));}}//显示行号列QHeaderView* headerView = table->verticalHeader();headerView->setHidden(true); //false 显示行号列  true HideQHeaderView* horizontalView = table->horizontalHeader();horizontalView->setHidden(true); //false 显示行号列  true Hidetable->setSelectionBehavior(QAbstractItemView::SelectRows);//选中的时候选中一行table->setSelectionMode(QAbstractItemView::SingleSelection);//只能选中单个目标table->setEditTriggers(QAbstractItemView::NoEditTriggers);//不能对表格内容进行修改table->setStyleSheet("QTableWidget{background:transparent; gridline-color:rgba(9, 107, 163, 100);color:#FFF;}"//"QTableWidget::Item{border:1px solid rgba(9, 107, 163, 1);border-bottom:1px solid rgba(9, 107, 163, 1);color: #FFFFFF;}""QTableWidget::Item:selected{background:transparent;color:rgba(4, 218, 255, 1);}");// 每一行点击connect(table, &QTableWidget::itemDoubleClicked, this, [&]() {qDebug() << "click";});QWidget* widget = new QWidget; widget->setStyleSheet("QWidget{border-image: url(:/images/Resource/image/CCendDetailsWidget/Legendbg1px.png);background-repeat:repeat-y; background-size:100% 1px;}");QHBoxLayout* layout = new QHBoxLayout(widget);layout->setContentsMargins(0, 0, 0, 0);layout->addWidget(table);

表格部分

#ifndef C_CEND_TABLE_DETAILS_INFO_TABLE_WIDGET_H
#define C_CEND_TABLE_DETAILS_INFO_TABLE_WIDGET_H#include <QWidget>
#include <QFile>
#include <QDebug>
#include <QMouseEvent>
#include <QAxWidget>
#include <QPushButton>
#include <QTextCodec>
#include <QGuiApplication>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonValue>
#include <Qjsonobject>
#include <Qlabel>
#include <QTableWidget>
#include <QTableWidgetItem>
#include <QHeaderView>
#include <QtCore>
#include <QtGui>
#include <QLabel>#ifdef WIN32
#pragma execution_character_set("utf-8")
#endifclass CCendDetailsInfoTableWidget : public QTableWidget
{Q_OBJECTpublic://构造函数,无实际内容,直接调用的构造函数CCendDetailsInfoTableWidget(QWidget *parent = 0) : QTableWidget(parent) { }// 析构函数CCendDetailsInfoTableWidget(int row, int column, QWidget *parent = 0): QTableWidget(row, column, parent) { }//设置某个单元格中的文字void setCellText( int cx, int cy, const QString &text, int alignment = Qt::AlignLeft, const QIcon icon = QIcon() );//在某个单元格中放置一张小图片void setCellPixmap(int cx, int cy, const QPixmap &pixmap,Qt::Alignment alignment = Qt::AlignCenter);//获取某个单元格中的字符串(如果没有,就返回一个空字符串)QString getCellText(int cx, int cy);//获取某个单元格的图片(如果没有,就返回一张空图片)QPixmap getCellPixmap(int cx, int cy);
};#endif // C_CEND_TABLE_DETAILS_INFO_TABLE_WIDGET_H

 cpp

#include "CCendDetailsInfoTableWidget.h"void CCendDetailsInfoTableWidget::setCellText(int cx, int cy, const QString &text, int alignment, const QIcon icon)
{//检查是否越界if( cx>=rowCount() || cy>=columnCount() ){qDebug() << "Fail, Out of Range";return;}//如果此单元格中已经有item了,就直接更改item中的内容//否则,新建一个itemQTableWidgetItem *titem = item(cx, cy);if( NULL == titem )titem = new QTableWidgetItem;titem->setText(text);titem->setTextAlignment(Qt::AlignCenter);//如果图标不为空,就为此item设置图标if( !icon.isNull() )titem->setIcon(icon);setItem(cx, cy, titem);
}void CCendDetailsInfoTableWidget::setCellPixmap(int cx, int cy, const QPixmap &pixmap,Qt::Alignment alignment)
{if( cx>=rowCount() || cy>=columnCount() ){qDebug() << "Fail, Out of Range";return;}//在item中设置图片有很多方法,但我还是觉得在其中放置带图片一个Label最简单QLabel *label = new QLabel(this);label->setAlignment(alignment);label->setPixmap(pixmap);setCellWidget(cx, cy, label);
}QString CCendDetailsInfoTableWidget::getCellText(int cx, int cy)
{QString result;if( cx>=rowCount() || cy>=columnCount() ){qDebug() << "Fail, Out of Range";return result;}QTableWidgetItem *titem = item(cx, cy);if( NULL != titem ){result = titem->text();}return result;
}QPixmap CCendDetailsInfoTableWidget::getCellPixmap(int cx, int cy)
{QPixmap result;if( cx>=rowCount() || cy>=columnCount() ){qDebug() << "Fail, Out of Range";return result;}QTableWidgetItem *titem = item(cx, cy);if( NULL == titem )return result;QLabel *label = dynamic_cast<QLabel*>( cellWidget(cx, cy) );result = label->pixmap();return result;
}

qss部分 

CCendDetailsInfoWidget QWidget#mainWidget
{background: transparent;
}CCendDetailsInfoWidget QWidget#headTopWidget
{border-image: url(":/images/Resource/image/CCendDetailsInfoWidget/headTopBg.png");
}CCendDetailsInfoWidget QPushButton#closeBtn
{border-image: url(":/images/Resource/image/searchListWidget/close.png");
}CCendDetailsInfoWidget QWidget#bodyWidget
{/*border-image: url(":/images/Resource/image/CCendDetailsWidget/Legendbg1px.png");   */background-repeat: repeat-y; /* 使水平方向重复 */background-size: 100% 1px; /* 宽度100%,高度1px */
}CCendDetailsInfoWidget  QLabel#label
{color: #ffffff;font-size: 16px;font-bold: 10px;text-align: center;
}/*滚动条*/
QScrollBar:horizontal
{border: none;border-radius: 2px;width: 4px;background-color: rgba(211, 226, 226, 100);margin: 2px 0 2px 0;
}QScrollBar::handle:horizontal
{border: none;border-radius: 2px;background-color: rgba(13, 133, 255, 51);width: 40px;margin: 0 0 0 0;subcontrol-position: bottom;subcontrol-origin: margin;
}QScrollBar:vertical
{border: none;border-radius: 2px;width: 4px;background-color: rgba(211, 226, 226, 100);margin: 2px 0 2px 0;
}QScrollBar::handle:vertical
{border: none;border-radius: 2px;background-color: rgba(13, 133, 255, 51);height: 40px;margin: 0 0 0 0;subcontrol-position: bottom;subcontrol-origin: margin;
}QTableWidget
{color:white;gridline-color: #096BA3;  //网格线border: 1px solid #096BA3;
}QTableWidget::item
{color:white;background:transparent;border: 1px solid #096BA3;
}
QTableWidget::item::selected
{color:white;background:transparent;
}
QTableWidget QScrollBar::vertical
{background:transparent;border:1 solid #096BA3;border-radius:8px;
}
QTableWidget QScrollBar::handle
{border-radius:6px;background:transparent;min-height:50px;
}
QTableWidget QScrollBar::add-line,QTableWidget QScrollBar::sub-line{border:none;}
QTableWidget QScrollBar::add-page,QTableWidget QScrollBar::sub-page{border:#0c161e;border-radius:10px;}

合并表格版本

json代码

[{"row": "12","column": "4","width":"100","table": [[ {"name": "取水水源名称","span": "0,1,1,1","align": "2","color": "255,255,255"},{"name": "河川水库","span": "0,1,1,3","align": "2","color": "4,218,255"}],[{"name": "河川水库","span": "1,1,1,1","align": "2","color": "255,255,255"},{"name": "河川水库","span": "1,1,1,3","align": "2","color": "4,218,255"}],[{"name": "河川水库","span": "2,1,1,1","align": "2","color": "255,255,255"},{"name": "河川水库","span": "2,1,1,3","align": "2","color": "4,218,255"}],[{"name": "河川水库","span": "3,1,1,1","align": "2","color": "255,255,255"},{"name": "河川水库","span": "3,1,1,3","align": "2","color": "4,218,255"}],[{"name": "河川水库","span": "4,1,1,1","align": "2","color": "255,255,255"},{"name": "河川水库","span": "4,1,1,3","align": "2","color": "4,218,255"}],[{"name": "河川水库","span": "5,1,1,1","align": "2","color": "255,255,255"},{"name": "河川水库","span": "5,1,1,3","align": "2","color": "4,218,255"}],[{"name": "河川水库","span": "6,1,1,1","align": "2","color": "255,255,255"},{"name": "河川水库","span": "6,1,1,3","align": "2","color": "4,218,255"}],[{"name": "设计饮水量","span": "7,1,1,1","align": "2","color": "255,255,255"},{"name": "设计饮水量","span": "7,1,1,1","align": "2","color": "255,255,255"},{"name": "设计饮水量","span": "7,1,1,1","align": "2","color": "255,255,255"},{"name": "设计饮水量","span": "7,1,1,1","align": "2","color": "255,255,255"}],[{"name": "17.0","span": "8,1,1,1","align": "2","color": "4,218,255"},{"name": "17.0","span": "8,1,1,1","align": "2","color": "4,218,255"},{"name": "17.0","span": "8,1,1,1","align": "2","color": "4,218,255"},{"name": "17.0","span": "8,1,1,1","align": "2","color": "4,218,255"}],[{"name": "输水干线上建筑物数量(处)","span": "9,0,1,4","align": "2","color": "255,255,255"}],[{"name": "水闸","span": "10,1,1,1","align": "2","color": "255,255,255"},{"name": "水闸","span": "10,1,1,1","align": "2","color": "255,255,255"},{"name": "水闸","span": "10,1,1,1","align": "2","color": "255,255,255"},{"name": "水闸","span": "10,1,1,1","align": "2","color": "255,255,255"}],[{"name": "17.0","span": "11,1,1,1","align": "2","color": "4,218,255"},{"name": "17.0","span": "11,1,1,1","align": "2","color": "4,218,255"},{"name": "17.0","span": "11,1,1,1","align": "2","color": "4,218,255"},{"name": "18.0","span": "11,1,1,1","align": "2","color": "4,218,255"}]]
}]

实现代码

void  CCendDetailsInfoWidget::initTableStyle(QTableWidget* table)
{//显示行号列table->horizontalHeader()->setVisible(false); // 隐藏行表头table->verticalHeader()->setVisible(false);   // 隐藏列表头table->setSelectionBehavior(QAbstractItemView::SelectRows);//选中的时候选中一行table->setSelectionMode(QAbstractItemView::SingleSelection);//只能选中单个目标table->setEditTriggers(QAbstractItemView::NoEditTriggers);//不能对表格内容进行修改//表列随着表格变化而自适应变化table->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);//表行随着表格变化而自适应变化table->verticalHeader()->setSectionResizeMode(QHeaderView::Stretch);table->setStyleSheet("QTableWidget{background:transparent; gridline-color:rgba(9, 107, 163, 100);color:#FFF;}""QTableWidget::Item:selected{background:transparent;color:rgba(4, 218, 255, 1);}");// 每一行点击connect(table, &QTableWidget::itemDoubleClicked, this, [&]() {qDebug() << "click";});QWidget* widget = new QWidget;widget->setStyleSheet("QWidget{border-image: url(:/images/Resource/image/CCendDetailsWidget/Legendbg1px.png);background-repeat:repeat-y; background-size:100% 1px;border-radius:5px;}");QHBoxLayout* layout = new QHBoxLayout(widget);layout->setContentsMargins(0, 0, 0, 0);layout->addWidget(table);ui->bodyWidget->layout()->addWidget(widget);
}void CCendDetailsInfoWidget::initJsonData()
{// 列表配置文件QJsonArray listJsonArray = getCfgJsonData("details_info_table_config.json");if (listJsonArray.isEmpty()){return;}int row = listJsonArray.at(0).toObject().value("row").toString().toInt();int column = listJsonArray.at(0).toObject().value("column").toString().toInt();int width = listJsonArray.at(0).toObject().value("width").toString().toInt();QJsonArray tableJsonArray = listJsonArray.at(0).toObject().value("table").toArray();CCendDetailsInfoTableWidget* table = new CCendDetailsInfoTableWidget(row, column);for (int i = 0; i < tableJsonArray.size(); ++i){QJsonArray jsonArray = tableJsonArray.at(i).toArray();for (int j = 0; j < jsonArray.size(); j++){QJsonValue jsonValue = jsonArray.at(j);QString name = jsonValue.toObject().value("name").toString();QStringList color = jsonValue.toObject().value("color").toString().split(",");QStringList span = jsonValue.toObject().value("span").toString().split(",");int align = jsonValue.toObject().value("align").toString().toInt();int alignment;if (align == 1){alignment = Qt::AlignLeft;}else if (align == 2){alignment = Qt::AlignCenter;}else {alignment = Qt::AlignRight;}table->setCellText(i, j, name, QColor(color[0].toInt(), color[1].toInt(), color[2].toInt()), alignment);table->setSpan(span[0].toInt(), span[1].toInt(), span[2].toInt(), span[3].toInt());}}initTableStyle(table);
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/770929.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

蓝桥杯2023年第十四届省赛真题-买瓜|DFS+剪枝

题目链接&#xff1a; 0买瓜 - 蓝桥云课 (lanqiao.cn) 蓝桥杯2023年第十四届省赛真题-买瓜 - C语言网 (dotcpp.com) &#xff08;蓝桥官网的数据要求会高一些&#xff09; 说明&#xff1a; 这道题可以分析出&#xff1a;对一个瓜有三种选择&#xff1a; 不拿&#xff0c…

Hbase解决ERROR: KeeperErrorCode = ConnectionLoss for /hbase/master报错

在使用hbase时出错&#xff0c;错误如下图&#xff1a; 错误原因&#xff1a; 返回去检查启动的Hadoop与zookeeper&#xff0c;发现zookeeper的状态不对&#xff0c;重新启动了一下zookeeper&#xff0c;确保所有机器的zookeeper都启动起来了就可以了。

微服务(基础篇-004-Feign)

目录 http客户端Feign Feign替代RestTemplate&#xff08;1&#xff09; Feign的介绍&#xff08;1.1&#xff09; 使用Feign的步骤&#xff08;1.2&#xff09; 自定义配置&#xff08;2&#xff09; 配置Feign日志的两种方式&#xff08;2.1&#xff09; Feign使用优化…

【C++】哈希应用之位图

&#x1f440;樊梓慕&#xff1a;个人主页 &#x1f3a5;个人专栏&#xff1a;《C语言》《数据结构》《蓝桥杯试题》《LeetCode刷题笔记》《实训项目》《C》《Linux》《算法》 &#x1f31d;每一个不曾起舞的日子&#xff0c;都是对生命的辜负 目录 前言 1.位图的概念 2.位…

解决“Pycharm中Matplotlib图像不弹出独立的显示窗口”问题

matplotlib的绘图的结果默认显示在SciView窗口中, 而不是弹出独立的窗口, 这样看起来就不是很舒服&#xff0c;不习惯。 通过修改设置&#xff0c;改成独立弹出的窗口。 File—>Settings—>Tools—>Python Scientific—>Show plots in toolwindow 将√去掉即可

初识C++(三)构造函数和析构函数

目录 一、构造函数&#xff1a; 1.构造函数的概念&#xff1a; 2.构造函数的特性&#xff1a; 3.构造函数的形式&#xff1a; 4.为什么要引出构造函数这一概念 5.默认构造函数包括&#xff1a; 6.对默认生成的构造函数不处理内置类型的成员这事的解决办法&#xff1a; …

【Python机器学习系列】skearn机器学习模型的保存---pickle法

这是我的第246篇原创文章。 一、引言 pickle是Python 的标准库&#xff0c;用于序列化对象。可以使用 pickle.dump()将模型保存到文件&#xff0c;然后使用 pickle.load()从文件中加载模型。 序列化&#xff1a;指将一个对象转换为字节流&#xff0c;能够存储在文件或网络上&…

计算机网络:现代通信的基石

&#x1f90d; 前端开发工程师、技术日更博主、已过CET6 &#x1f368; 阿珊和她的猫_CSDN博客专家、23年度博客之星前端领域TOP1 &#x1f560; 牛客高级专题作者、打造专栏《前端面试必备》 、《2024面试高频手撕题》 &#x1f35a; 蓝桥云课签约作者、上架课程《Vue.js 和 E…

异地两台电脑如何共享文件?

在当前数字化时代&#xff0c;人们对于数据的使用和管理变得越来越便捷。由于工作和生活的需要&#xff0c;我们常常需要在异地的电脑间共享文件。这给我们的工作和生活带来了一定程度的不便。有没有一种便捷的方法可以让异地的电脑实现文件的共享呢&#xff1f;答案是肯定的。…

知识图谱-图数据库-neo4j (1)踩坑记录

1、neo4j 安装 材料 &#xff1a; openjdk11 (neo4j 最低jdk版本要求) neo4j-community-4.4.30 CentOS 7.8 Release Date: 25 January 2024 Neo4j 4.4.30 is a maintenance release with many important improvements and fixes. Neo4j Deployment Center - Graph Database…

【前端面试3+1】01闭包、跨域

一、对闭包的理解 定义&#xff1a; 闭包是指在一个函数内部定义的函数&#xff0c;并且该内部函数可以访问外部函数的变量。闭包使得函数内部的变量在函数执行完后仍然可以被访问和操作。 特点&#xff1a; 闭包可以访问外部函数的变量&#xff0c;即使外部函数已经执行完毕。…

Linux 搭建jenkins docker

jekin docker gitee docker 安装 jenkins docker run -d --restartalways \ --name jenkins -uroot -p 10340:8080 \ -p 10341:50000 \ -v /home/docker/jenkins:/var/jenkins_home \ -v /var/run/docker.sock:/var/run/docker.sock \ -v /usr/bin/docker:/usr/bin/docker je…

QT数据类型和容器用法

Qt库提供了基于通用模板的容器类, 这些类可用于存储指定类型的数据项&#xff0c;Qt中这些容器类的设计比STL容器更轻&#xff0c;更安全且更易于使用。容器类也都是隐式共的&#xff0c;它们是可重入的&#xff0c;并且已针对速度/低内存消耗和最小的内联代码扩展进行了优化&a…

【解析几何】 【多源路径】 【贪心】1520 最多的不重叠子字符串

作者推荐 视频算法专题 本身涉及知识点 解析几何 图论 多源路径 贪心 LeetCode1520. 最多的不重叠子字符串 给你一个只包含小写字母的字符串 s &#xff0c;你需要找到 s 中最多数目的非空子字符串&#xff0c;满足如下条件&#xff1a; 这些字符串之间互不重叠&#xff0…

Wireshark使用实训---分析IP包

1.Wireshark简介和作用 Wireshark是一个开源的网络分析工具&#xff0c;用于捕捉和分析网络数据包。它可以帮助网络管理员和安全专家监控和解决网络问题&#xff0c;同时也可以用于学习和教学网络通信原理。 Wireshark可以在网络中捕获和分析传输的数据包&#xff0c;包括协议…

【Java初阶(五)】类和对象

❣博主主页: 33的博客❣ ▶文章专栏分类: Java从入门到精通◀ &#x1f69a;我的代码仓库: 33的代码仓库&#x1f69a; 目录 1. 前言2.面向对象的认识3.类的认识4. 类的实例化4.1什么是实例化4.2类和对象的说明 5.this引用6.对象初始化6.1 构造方法 7.static关键字8.代码块8.1 …

PTAxt的考研路

xt是我院19级专业第一&#xff0c;但他认为保研并不能展示他全部的实力&#xff0c;所以他在22年初试一结束就加入了23考研的队伍中&#xff0c;并且他为了填补我院近些年来无北大研究生的空白&#xff0c;毅然决然决定扛起19级的大旗&#xff0c;在学校百年华诞之际献上他最诚…

光明源@智慧公厕赋能“厕所革命”主要体现在哪些方面?

当我们提及厕所&#xff0c;不再仅是简单的卫生设施&#xff0c;而是一种对生活品质的关怀与呵护。智慧公厕&#xff0c;作为厕所革命的引领者&#xff0c;以其独特的拟人魅力&#xff0c;彰显着人性化关怀的新风尚。今日&#xff0c;让我们一同探索&#xff0c;智慧公厕是如何…

数据库备份工具(实现数据定时覆盖)

数据库备份工具&#xff08;实现数据定时覆盖&#xff09; 永远热爱&#xff0c;永远执着&#xff01; 工具介绍 自动化测试数据库更新调度程序 这段 Python 脚本自动化了每天定时从生产数据库更新测试数据库的过程。它利用了 schedule 库来安排并执行每天指定时间的更新任务…

在for循环加判断条件当条件都满足时,同时显现的解决方法

一、代码示例 function fu(s) {str ;ste ;console.log(s);let Things s;for (let i 0; i < Things.length; i) {if (Things[i].pid kk) {console.log(Things[i].pid);ste <div class"commodity_nei"><div class"zxc_pic"><div cl…