QTableview实现鼠标放上面显示不同颜色

0 683
索鸟 2021-04-06
需要:0索币

需要实现QTableview鼠标悬浮时当前行高亮显示。但是实现的过程中发现一开始的方案不能用,必须捕捉鼠标的move事件通过Delegate自己绘制背景实现。一开始想通过重载Delegate里面的editorEvent捕捉。后面发现鼠标的move事件可以捕捉的到,但是leave事件获取不到,这样就会造成鼠标移出表格始终又一行高亮。后面只能通过继承QTableview捕捉到鼠标事件,然后通过delegate绘制的方法实现。

初始方案

在Qt4中可以直接设置item的悬停(hover)事件,如下所示:


QTableView::item:hover {

    background-color: rgba(200,200,220,255);

}        

然后在tableview的属性中设置selectionMode和selectionBehavior的属性为每次选中一行。


ui.TableView->setSelectionBehavior(QAbstractItemView::SelectRows);

ui.TableView->setSelectionMode(QAbstractItemView::SingleSelection);

结果发现在Qt5中样式表设置的样式不能生效。所以只能另找方案。


解决方案确定

在stackoverflow上面查了有人提供了下面的方案:


You can use delegates to draw the row background…

You will need to set the row to highlight in the delegate and based on that, do the highlighting.

Catch the signal of current row. Iterate over the items in that row and set background for each item.

翻译一下就是捕捉当前鼠标悬浮(hover)行的信号,使用委托绘制行背景。


绘制背景

绘制背景的话只能通过重载QStyledItemDelegate里面的paint函数进行选中行的背景绘制。

捕捉当前鼠标悬浮的信号

在获取鼠标hover的信号的过程中遇到了一些问题。一开始希望通过QStyledItemDelegate里面的editorEvent,捕获里面event的动作获取悬浮信号。

bool CRoomMemDelegate::editorEvent(QEvent * event, QAbstractItemModel * model, const QStyleOptionViewItem & option, const QModelIndex & index)

{

if (event->type() == QEvent::MouseMove)//鼠标移动事件

{

    m_selectionRow = index.row();

}

else if(event->type() == QEvent::leave)//离开事件

{

     m_selectionRow = -1;

}

return false;

}

如上尝试了通过m_selectionRow 保存当前悬停的行,然后在paint函数里面进行绘制背景。不过调试中发现mousemove动作可以捕获的到,但是leave事件得不到。所以会导致无论如何都会有一行被选中。


为了能够捕捉到鼠标的事件,子类话了QTableview,然后通过信号告诉QStyledItemDelegate鼠标的move/leave的动作。这个信号把悬停的当前行的信息发送出去。QStyledItemDelegate获取到hoving的行,保存当前行的信息并触发paint的动作进行绘制。具体的操作代码如下:


//1: Tableview :

void TableView::mouseMoveEvent(QMouseEvent *event)

{

    QModelIndex index = indexAt(event->pos());

    emit hoverIndexChanged(index);

    ...

}

//2.connect signal and slot

    connect(this,SIGNAL(hoverIndexChanged(const QModelIndex&)),delegate_,SLOT(onHoverIndexChanged(const QModelIndex&)));


//3.onHoverIndexChanged

void TableViewDelegate::onHoverIndexChanged(const QModelIndex& index)

{

    hoverrow_ = index.row();

}


//4.in Delegate paint():

void TableViewDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const

{

...

    if(index.row() == hoverrow_)

    {

        //HERE IS HOVER COLOR

        painter->fillRect(option.rect, kHoverItemBackgroundcColor);

    }

    else

    {

        painter->fillRect(option.rect, kItemBackgroundColor);

    }

...

}

上面的代码少了一个步骤就是触发paint绘制的动作,这个可以调用model里面的dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector &roles = …)函数刷新对应行的信息。

原文链接:https://blog.csdn.net/u013394556/article/details/103313782

回帖
  • 消灭零回复
相关主题
2020年最新最新Kubernetes视频教程(K8s)教程 2
程序员转型之制作网课变现,月入过万告别996 1
索鸟快传2.0发布啦 1
两个不同网络的电脑怎么实现文件的互相访问呢? 1
网盘多账号登录软件 1
Java实战闲云旅游项目基于vue+element-ui 1
单点登录技术解决方案基于OAuth2.0的网关鉴权RSA算法生成令牌 1
QT5获取剪贴板上文本信息QT设置剪贴板内容 1
springboot2实战在线购物系统电商系统 1
python web实战之爱家租房项目 1
windows COM实用入门教程 1
C++游戏开发之C++实现的水果忍者游戏 1
计算机视觉库opencv教程 1
node.js实战图书管理系统express框架实现 1
C++实战教程之远程桌面远程控制实战 1
相关主题
PHP7报A non well formed numeric value encountered 0
Linux系统下关闭mongodb的几种命令分享 0
mongodb删除数据、删除集合、删除数据库的命令 0
Git&Github极速入门与攻坚实战课程 0
python爬虫教程使用Django和scrapy实现 0
libnetsnmpmibs.so.31: cannot open shared object file 0
数据结构和算法视频教程 0
redis的hash结构怎么删除数据呢? 0
C++和LUA解析器的数据交互实战视频 0
mongodb errmsg" : "too many users are authenticated 0
C++基础入门视频教程 0
用30个小时精通C++视频教程可能吗? 0
C++分布式多线程游戏服务器开发视频教程socket tcp boost库 0
C++培训教程就业班教程 0
layui的util工具格式时间戳为字符串 0
C++实战教程之远程桌面远程控制实战 1
网络安全培训视频教程 0
LINUX_C++软件工程师视频教程高级项目实战 0
C++高级数据结构与算法视频教程 0
跨域问题很头疼?通过配置nginx轻松解决ajax跨域问题 0