问题描述
我使用的是 C++ Qt5.目前我有一个 QStandardItemModel 显示为具有多行和多列的 QTreeView.我知道使用 setStyleSheet(),但这似乎只是改变了行的颜色.我正在寻找的是当鼠标悬停在一行上时,会调用一个函数,然后我可以用它来操纵我的游戏.
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.
推荐答案
您可以使用委托 (http://doc.qt.io/qt-5/qtwidgets-itemviews-stardelegate-example.html) 和 QStyle::State_MouseOver 检查鼠标是否悬停在一行上.您应该覆盖 paint 方法.
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);
}
}
这篇关于QTreeView,鼠标悬停在一行上时如何调用动作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!