QTableWidget实现鼠标移动整行颜色变化
来源:互联网 发布:死亡扳机2机枪 mac 编辑:程序博客网 时间:2024/06/08 02:49
QTableWidget实现鼠标移动整行颜色变化
困扰了本人很久的一个问题就是QTableWidget的qss样式只能提供每个item:hover时的样式变化,而我们想要实现的是一整行的样式变化,在不断的坚持下今天终于解决了,今天写下来方便大家查阅,希望能够帮到大家。
- *主要的方法就是新建一个新类继承自QTableWidget,我这里的类名为SWTableWidget,然后在头文件和源文件中填写代码,之后在ui设计中拖入一个QTableWidget控件,再将该控件提升为我们新建的类SWTableWidget,具体步骤我在这里就不啰嗦啦,下面直接看代码
代码块
#ifndef SWTABLEWIDGET_H#define SWTABLEWIDGET_H#include <QWidget>#include <QTableWidget>#include <QColor>class SWTableWidget : public QTableWidget{ Q_OBJECTpublic: explicit SWTableWidget(QWidget *parent = nullptr);protected: void leaveEvent(QEvent *event);//鼠标移出事件public slots: void cellEntered(int,int);//鼠标移动事件,返回item的行和列号private: QColor lastRowBkColor;//存储item之前的颜色,这里是透明的,默认设置为透明 int previousColorRow;// 鼠标移动过的上一行的行号 void setRowColor(int,QColor);};#endif // SWTABLEWIDGET_H
代码块语法遵循标准markdown代码,例如:
#include "swtablewidget.h"#include <QDebug>#include <QMouseEvent>SWTableWidget::SWTableWidget(QWidget *parent) : QTableWidget(parent){ this->setMouseTracking(true);//设置可捕获鼠标移动事件,很重要 lastRowBkColor = QColor(0x00,0xff,0x00,0x00);//透明颜色 previousColorRow = -1;/*******************本段代码只是测试用,真正使用时需注释掉********************************/ this->setRowCount(10); //设置行数为10 this->setColumnCount(5); //设置列数为5 for(int i = 0; i < 10; i ++) { for(int j = 0; j < 5; j ++) { QTableWidgetItem *item = new QTableWidgetItem(); this->setItem(i,j,item); } }/*******************本段代码只是测试用,真正使用时需注释掉********************************/ this->setSelectionBehavior(QAbstractItemView::SelectRows); //设置整行选择 //this->setStyleSheet("selection-background-color:rgba(0,0,0,50)"); //设置选中行颜色 this->setEditTriggers(QAbstractItemView::NoEditTriggers); //禁止编辑表格 connect(this,SIGNAL(cellEntered(int,int)),this,SLOT(cellEntered(int,int)));}void SWTableWidget::leaveEvent(QEvent *event){ QTableWidgetItem *item = 0; //还原上一行的颜色 item = this->item(previousColorRow, 0); if (item != 0) { this->setRowColor(previousColorRow, lastRowBkColor); }}void SWTableWidget::cellEntered(int row, int column){ QTableWidgetItem *item = 0; //还原上一行的颜色 item = this->item(previousColorRow, 0); if (item != 0) { this->setRowColor(previousColorRow, lastRowBkColor); } //设置当前行的颜色 item = this->item(row, column); if (item != 0 && !item->isSelected()) { this->setRowColor(row, QColor(193,210,240)); } //设置行的索引 previousColorRow = row;}void SWTableWidget::setRowColor(int row, QColor color){ for (int col=0; col<this->columnCount(); col++) { QTableWidgetItem *item = this->item(row, col); item->setBackgroundColor(color); }}
效果图如下:
脚注
本示例参考其他博友的博客,由本人能力有限,如有错误的地方请多多指教
注:本博客未经博主允许,请不要转载
阅读全文
0 0
- QTableWidget实现鼠标移动整行颜色变化
- 控制鼠标移动时的颜色变化
- Jquery 实现表格颜色交替变化,点击选中行,鼠标移过颜色变化。
- Jquery 实现表格颜色交替变化,点击选中行,鼠标移过颜色变化。
- javascript学习实录 之七(实现鼠标移动上去后表格颜色变化)
- jsp页面鼠标移动样式和颜色变化
- javascript学习实录 之七(实现鼠标移动上去后表格颜色变化) -- 刘小小尘
- 鼠标移动背景变化
- Jquery 实现表格颜色交替变化鼠标移过颜色变化实例
- 鼠标移动变颜色
- 鼠标移动变颜色
- 鼠标经过后颜色变化
- 在树型控件中移动鼠标产生字体及颜色的变化
- ASP的GRIDVIEW中行颜色随鼠标移动变化(半原创)
- jquery隔行换色和鼠标移动、离开当前行颜色变化
- VirtualTreeview鼠标移动到相应节点时,节点背景颜色发生变化
- 鼠标放到哪一行,哪一行颜色发生变化
- 鼠标移动上鼠标指针各种变化
- 测试->转载博文
- JS String对象slice substr substring的对比
- Java线程面试题(03) Java中的volatile如何工作? Java中的volatile关键字示例
- python面向对象编程
- vim结合findstr实现windows下快速在文件中查找
- QTableWidget实现鼠标移动整行颜色变化
- javaweb在阿里云centos7下搭建mysql5.6简单的主从结构
- spring 事务管理 mysql默认事务
- Blade源码深入探索1--注册路由之ioc容器
- 贪心算法
- 使用psql命令基于udp(unix-domain-socket)协议连接postgresql数据库
- JVM GC收集器及其收集算法
- 微信小程序-gulp构建
- 如何选择一款合适的核心板进行开发