如何在 QTableWidget 中突出显示鼠标悬停时的整行:Qt5

How to highlight the entire row on mouse hover in QTableWidget: Qt5(如何在 QTableWidget 中突出显示鼠标悬停时的整行:Qt5)
本文介绍了如何在 QTableWidget 中突出显示鼠标悬停时的整行:Qt5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 QTableWidget 中突出显示鼠标悬停时的行.

I want to highlight the row on mouse hover in my QTableWidget.

当我将鼠标悬停时,只有一个单元格突出显示.

When I hover the mouse, only single cell highlighted.

我已经尝试过这种方法:

I have tried this approach :

bool MyTabWidget::eventFilter(QObject *target, QEvent *event)
{
    if( target == ui->MyTableWidget )
    {
        //Just to print the event type
        qDebug() <<"EventType : "<<event->type();
    }
}

输出:EventType:13.

`(13 = QEvent::Move)`

我已经完成了谷歌搜索.但没有得到任何适当的解决方案.

I have done lost of googling. but not get any proper solution.

有没有其他方法可以满足我的要求(在鼠标悬停时突出显示整行)?

Is there any other approach to fulfill my requirment (to highlight entire row on mouse hover)?

请帮忙.提前致谢.

请参阅下面的屏幕截图以获得更清晰的信息.

Please refer below screen shot for more clear.

这是我的 QTableWidget我想在鼠标悬停时更改该红色边框(已编辑)行的背景颜色.

This is my QTableWidget I want to change the background color of that red boarder(edited) row on mouse hover.

推荐答案

这是我的实现,效果很好.首先你应该继承 QTableView/QTabWidget ,在 mouseMoveEvent/dragMoveEvent 函数中向 QStyledItemDelegate 发出信号.这个信号将发送悬停索引.

Here is my implementation,it works well.First you should subclass QTableView/QTabWidget ,emit a signal to QStyledItemDelegate in mouseMoveEvent/dragMoveEvent function .This signal will send the hovering index.

在 QStyledItemDelegate 中,使用成员变量 hover_row_(在插槽中更改为绑定到上述信号)告诉绘制函数哪一行被悬停.

In QStyledItemDelegate ,use a member variable hover_row_(changed in a slot bind to above signal) to tell paint function which row is be hovered.

代码示例如下:

//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);
    }
...
}

这篇关于如何在 QTableWidget 中突出显示鼠标悬停时的整行:Qt5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Rising edge interrupt triggering multiple times on STM32 Nucleo(在STM32 Nucleo上多次触发上升沿中断)
How to use va_list correctly in a sequence of wrapper functions calls?(如何在一系列包装函数调用中正确使用 va_list?)
OpenGL Perspective Projection Clipping Polygon with Vertex Outside Frustum = Wrong texture mapping?(OpenGL透视投影裁剪多边形,顶点在视锥外=错误的纹理映射?)
How does one properly deserialize a byte array back into an object in C++?(如何正确地将字节数组反序列化回 C++ 中的对象?)
What free tiniest flash file system could you advice for embedded system?(您可以为嵌入式系统推荐什么免费的最小闪存文件系统?)
Volatile member variables vs. volatile object?(易失性成员变量与易失性对象?)