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.
This is my QTableWidget
I want to change the background color of that red boarder(edited) row on mouse hover.
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);
}
...
}
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&)));
}
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 needs a QTableView, you need to disable your current hover effect and override the paint and mousemove method. On your mousemove 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...
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);
}