QTableView:如何将鼠标悬停在整行上? [英] QTableView: how to hover an entire row on mouse over?

查看:1450
本文介绍了QTableView:如何将鼠标悬停在整行上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我分类了QTableView,QAbstractTableModel和QItemDelegate。我可以将一个单元格悬停在鼠标上:

  void SchedulerDelegate :: paint(QPainter * painter,const QStyleOptionViewItem&选项,const QModelIndex& index)const 
{
...

if(option.showDecorationSelected&&&((option.state& QStyle :: State_Selected))
{
QColor color(255,255,130,100);
QColor colorEnd(255,255,50,150);
QLinearGradient渐变(option.rect.topLeft(),option.rect.bottomRight());
gradient.setColorAt(0,color);
gradient.setColorAt(1,colorEnd);
QBrush画笔(渐变);
painter-> fillRect(option.rect,brush);
}

...
}

...但我无法弄清楚,如何悬停整行。有人可以帮助我提供示例代码吗?

解决方案

有2种方法..


1)您可以使用委托来绘制行背景......

您需要设置行以在委托中突出显示,并基于此
做突出显示。

<2>捕捉当前行的信号。迭代该行中的项目

为每个项目设置背景。


您也可以试试style sheet:

  QTableView :: item:hover {
background-color:#D3F1FC;
}

希望它对你们有用。


I subclassed QTableView, QAbstractTableModel, and QItemDelegate. I am able to hover a single cell on mouse over:

void SchedulerDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    ...

    if(option.showDecorationSelected &&(option.state & QStyle::State_Selected))
{
    QColor color(255,255,130,100);
    QColor colorEnd(255,255,50,150);
    QLinearGradient gradient(option.rect.topLeft(),option.rect.bottomRight());
    gradient.setColorAt(0,color);
    gradient.setColorAt(1,colorEnd);
    QBrush brush(gradient);
    painter->fillRect(option.rect,brush);
}

    ...
}

... but I cannot figure out, how to hover an entire row. Can Someone help me with sample codes?

解决方案

There are 2 ways..

1) 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.

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

you can also tried style sheet:

QTableView::item:hover {
    background-color: #D3F1FC;
}        

Hope, It will usefull to you guys.

这篇关于QTableView:如何将鼠标悬停在整行上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆