本文介绍了如何在 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 QTableWidgetI 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 05:01