选择行为被设置为选择行,但只有悬停的单元格被突出显示。有什么方法可以突出整行?QTableView如何突出显示鼠标悬停的整个行?
0
A
回答
1
有2种方式..
1)您可以使用委托绘制行背景...
您将需要设置该行的委托,以突出和此基础上, 突出显示。2)抓住当前行的信号。迭代该行中的项目 和 为每个项目设置背景。
希望它会对你们有用。
1
首先,您的子类QTableWidget/QTableView
和重新实现mouseMoveEvent
和leaveEvent
。
在custom_table_widget.cpp
,你应该有:
...
CustomTableWidget::CustomTableWidget(QWidget *parent) :
QTableWidget(parent)
{
setMouseTracking(true); // receives mouse move events even if no buttons are pressed.
}
void CustomTableWidget::mouseMoveEvent(QMouseEvent *event)
{
// detect where the mouse cursor is relative to our custom table widget
QModelIndex index = indexAt(event->pos());
emit hoverIndexChanged(index);
}
void CustomTableWidget::leaveEvent(QEvent *event)
{
// detect when the mouse cursor leaves our custom table widget
emit leaveTableEvent();
viewport()->update();
}
...
接下来,你继承QStyledItemDelegate
。重新执行paint
方法并添加两个槽以修改悬停行。在row_hover_delegate.cpp
,你应该有:
...
void RowHoverDelegate::onHoverIndexChanged(const QModelIndex& item) {
hovered_row_ = item.row();
}
void RowHoverDelegate::onLeaveTableEvent() {
hovered_row_ = -1;
}
void RowHoverDelegate::paint(QPainter *painter,
const QStyleOptionViewItem &option,
const QModelIndex &index) const {
QStyleOptionViewItem opt = option;
if(index.row() == hovered_row_) {
opt.state |= QStyle::State_MouseOver;
} else {
opt.state &= ~QStyle::State_MouseOver;
}
QStyledItemDelegate::paint(painter, opt, index);
}
...
最后,连接的信号/插槽,并设置项目委托:
connect(my_custom_table_widget,
&CustomTableWidget::hoverIndexChanged,
my_row_hover_delegate,
&RowHoverDelegate::onHoverIndexChanged);
connect(my_custom_table_widget,
&CustomTableWidget::leaveTableEvent,
my_row_hover_delegate,
&RowHoverDelegate::onLeaveTableEvent);
my_custom_table_widget->setItemDelegate(my_row_hover_delegate);
+0
谢谢你的代码解释,但并不是真的有必要。 – 0xbaadf00d
相关问题
- 1. 如何突出显示鼠标悬停的标题列?
- 2. 在悬停上突出显示整行
- 3. 如何检测并突出显示鼠标悬停的矩形
- 4. 如何突出显示鼠标悬停时的JPanel?
- 5. 如何突出显示kendo datepicker在鼠标悬停的一周
- 6. QTableView:如何将鼠标悬停在整行上?
- 7. java:在鼠标悬停和鼠标悬停时突出显示dom元素
- 8. 试图让整个表格行在鼠标悬停时突出显示
- 9. 突出显示鼠标悬停时的表格行
- 10. 突出鼠标悬停表
- 11. 如何在鼠标悬停到VBA时启用突出显示?
- 12. 如何在鼠标悬停上突出显示文本
- 13. nvd3:如何突出鼠标悬停整个堆积酒吧?
- 14. 在鼠标悬停上突出显示文本行
- 15. 如何在Silverlight数据网格中删除鼠标悬停突出显示和选定的行突出显示
- 16. 如何突出显示没有鼠标悬停在图表中的行?
- 17. 如何禁用鼠标悬停在单元格gwt中的行突出显示
- 18. 如何在Extjs 4.0中的鼠标悬停上突出显示一个网格?
- 19. 鼠标悬停时突出显示图像的某些部分
- 20. 突出显示鼠标悬停周末的事件
- 21. wpf按钮的边距在鼠标悬停时突出显示
- 22. d3鼠标悬停的分组条形图突出显示组
- 23. 如何显示在鼠标悬停时
- 24. MFC/WIN32:鼠标悬停突出显示在列表控件中
- 25. 禁用鼠标悬停突出显示工具栏按钮(winapi)
- 26. 为什么ListBoxItem在鼠标悬停时不会突出显示?
- 27. 鼠标悬停突出显示页面元素
- 28. Zurb Foundation on Rails鼠标悬停黑色突出显示
- 29. leafletjs:在鼠标悬停上突出显示折线
- 30. 鼠标悬停在子控件上时突出显示控件
.Could请您给一些关于第一种方法 – IMAN4K