Ender
Ender

Reputation: 1778

QTreeView, how to call an action when mouse hovers over a row?

I am using C++ Qt5. Currently I have a QStandardItemModel being displayed as a QTreeView with multiple rows and columns. I am aware of using setStyleSheet(), but that just seems to change the color of the row. What I'm looking for is when the mouse hovers over a row, a function is called which I can then use to manipulate my game.

Upvotes: 1

Views: 1650

Answers (1)

Andrey Semenov
Andrey Semenov

Reputation: 971

You can use delegate (http://doc.qt.io/qt-5/qtwidgets-itemviews-stardelegate-example.html) and QStyle::State_MouseOver to check if mouse over a row. You should override paint method.

void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const {
    if(index.row() == 2 && (option.state & QStyle::State_MouseOver)) {
        painter->fillRect(option.rect, Qt::blue);
    } else {
        QStyledItemDelegate::paint(painter, option, index);
    }
}

Upvotes: 3

Related Questions