QTableView: how to hover over the entire row?

I have subclassed QTableView, QAbstractTableModel and QItemDelegate. I can point one cell to the mouse:

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 can’t figure out how to put the whole line. Can someone help me with code examples?

+3
source share
2 answers

There are two ways.

1) You can use delegates to draw the background of the string ... You will need to set the string to highlight in the delegate and based on this, make a selection.

2) Catch the signal of the current line. Iterate the elements in this line and also set the background for each element.

you can also try the stylesheet:

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

, , .

+1

, . QTableView/QTabWidget, QStyledItemDelegate mouseMoveEvent/dragMoveEvent. .

QStyledItemDelegate - hover_row_ ( ), , .

:

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

All Articles