Qt委托设置鼠标悬停状态的复选框
Qt delegate setting mouseover state for checkbox
我在QTableview中使用的委托中有一个paint方法,我在其中向单元格添加了一个复选框指示器。当我通过检查选项进入单元格时,很容易设置复选框的鼠标悬停状态。状态QStyle::State_MouseOver标志,但我理想情况下需要做的是,当鼠标指针在指示器本身之上,而不仅仅是在单元格周围悬停时,只设置复选框指示器的鼠标悬停状态。不幸的是,油漆方法仅在当前从一个单元格移动到另一个单元格时触发,因此我需要一些关于如何做到这一点的指针。
代码如下(其中mouse_pointer_是最后存储的鼠标坐标):
void
CheckBoxDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option,
const QModelIndex& index) const
{
// Draw our checkbox indicator
bool value = index.data(Qt::DisplayRole).toBool();
QStyleOptionButton checkbox_indicator;
// Set our button state to enabled
checkbox_indicator.state |= QStyle::State_Enabled;
checkbox_indicator.state |= (value) ? QStyle::State_On : QStyle::State_Off;
// Get our dimensions
checkbox_indicator.rect = QApplication::style()->subElementRect(QStyle::SE_CheckBoxIndicator, &checkbox_indicator, NULL );
// Position our indicator
const int x = option.rect.center().x() - checkbox_indicator.rect.width() / 2;
const int y = option.rect.center().y() - checkbox_indicator.rect.height() / 2;
checkbox_indicator.rect.moveTo(x, y);
if (checkbox_indicator.rect.contains(mouse_position_)) {
checkbox_indicator.state |= QStyle::State_MouseOver;
}
QApplication::style()->drawControl(QStyle::CE_CheckBox, &checkbox_indicator, painter);
}
经过一番调查,我发现问题是我的editorEvent()方法需要返回true(表明单元格内容已经更改)来强制重新绘制单元格,从而设置所选状态。代码如下:
bool CheckBoxDelegate::editorEvent(QEvent* event, QAbstractItemModel* model, const QStyleOptionViewItem& option, const QModelIndex& index)
{
bool repaint_cell = false;
QMouseEvent *e = static_cast<QMouseEvent *> (event);
mouse_position_ = e->pos();
// We need to check if the mouse pointer is hovering within
// the checkbox indicator area of the table cell
QStyleOptionButton checkbox_indicator;
checkbox_indicator.rect = QApplication::style()->subElementRect( QStyle::SE_CheckBoxIndicator, &checkbox_indicator, NULL );
const int x = option.rect.center().x() - checkbox_indicator.rect.width() / 2;
const int y = option.rect.center().y() - checkbox_indicator.rect.height() / 2;
checkbox_indicator.rect.moveTo(x, y );
if (checkbox_indicator.rect.contains(mouse_position_)) {
repaint_cell = true;
// Check if the user has clicked in this area
if (e->button() == Qt::LeftButton) {
switch(event->type()) {
case QEvent::MouseButtonRelease:
// Update model data in here
break;
default:
break;
}
}
}
return repaint_cell;
}
更简单
bool hover = option.state & QStyle::State_MouseOver;
if (hover) {
painter->fillRect(r, QColor(0,0,0,10));
}
相关文章:
- 根据变量使Qt UI中的复选框为已选中/未选中
- "动态创建的事件处理程序"复选框
- 有没有办法在取消选中复选框时隐藏按钮?
- 如何删除Qt设计器中自定义插件的QString属性的"translatable"复选框?
- 设置复选框选项C++
- 如何在所有者描述的 MFC CMenu 派生菜单中显示复选框?
- 如何在MFC对话框应用程序中默认将复选框设置为选中?
- 如何使复选框独占
- AccessObjectFromPoint 返回客户端对象而不是复选框
- Windows 32 API:2个复选框作为一个
- 如何在 QT3 中将复选框添加到 QFileDialog 窗口
- 在 QComboBox 中切换复选框
- 在使用Qt修改任何复选框状态时执行操作
- QTableWidget复选框获取状态和位置
- Qt QStandardItem setCheckable(true) 不保存复选框状态
- 如何在c++中读取/写入excel复选框状态
- 我可以重置wxWidgets菜单中的复选框状态吗?
- Qt委托设置鼠标悬停状态的复选框
- 如何使复选框在单击时更改其状态
- 正确确定自定义绘制的复选框状态