7

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();
    }
}

Output : 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 help. Thank in advance.

EDIT:

Please refer below screen shot for more clear.

enter image description here

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

AB Bolim
  • 1,997
  • 2
  • 23
  • 47

5 Answers5

2

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.

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.

Here is the code examaple:

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

I had similar task and answer from baixiangcpp helped me, but it worked only when mouse button was pressed, not on simple hovering. I resolved this issue with help from user mrjj on qt forum, who suggested I should set "mouseTracking" property in TableView (CustomView in my case) to "true".

CustomView::CustomView(QWidget *parent) : QTableView(parent)
{
    this->setMouseTracking(true);
    connect(this,SIGNAL(hoverIndexChanged(const QModelIndex&)),parent,SLOT(onHoverIndexChanged(const QModelIndex&)));
  }
max paint
  • 49
  • 6
0

It's not the correct way to solve the problem, but if you would like to continue using QTableWidget you can just show the verticalheader and click above them to highlight its specific row.

-2

I'm adding another answer cause it's too long:

Ok, you're right, this is happening on QTableView. Now the question is, why you need a QTableView? If you just need a resume like the one you posted there, you can use a QTreeView, that instead of the QTableView, supports hovering on the entire row, instead of a single cell.

If you absolutely need a QTableView, you need to disable your current hover effect and override the paint and mouseMoveEvent method. On your mouseMoveEvent method calculate the row under the mouse using QTableView::rowAt(y) (also remember to map your mouse coords to the Widget relative coords), and store an index, if it changes from the previous one, invalidate the entire table. On the paint event, just paint a rect around the row manually after calling the base class paint event.

congard
  • 945
  • 2
  • 10
  • 28
Leonardo Bernardini
  • 1,076
  • 13
  • 23
-4

Haven't played with QT5 still, but with QT4 this is super-easy using a style sheet:

QTableView::item:hover {
    background-color: rgba(200,200,220,255);
}        
Leonardo Bernardini
  • 1,076
  • 13
  • 23
  • 3
    Thanks #Leonardo Bernardini for your answer, But It wont work. By applying StyleSheet, only single cell highlighted rather then entire row... – AB Bolim Apr 16 '14 at 13:49
  • Then your problem is with the selection behaviour, use tableView->setSelectionBehavior(QAbstractItemView::SelectRows); – Leonardo Bernardini Apr 16 '14 at 17:07
  • I want to highlight row on mouse hover in my `QTableWidget`. Above solution is not applicable for that... – AB Bolim Apr 17 '14 at 06:39
  • 1
    if you want to have single-cell selection, but hover on the entire row , your only solution is overriding your own paint function.. I confirm that, if you change the selection behaviour for full row, the hover also applies to the full row... – Leonardo Bernardini Apr 17 '14 at 12:55
  • May be there is some confusion, On mouse hover within `QTableWidget` that particular cell is highlighted. But, I want to highlighted that entire row on mouse hover. Hope now you got my problem.. – AB Bolim Apr 17 '14 at 13:05