我有一个项目代理,当鼠标事件悬停在图标上时,我将其光标更改为Qt :: PointingHandCursor。当其离开时,我将其设置回Qt :: ArrowCursor。它工作得很好。
问题是除了当鼠标悬停在图标上时,它总是停留在Qt :: ArrowCursor上,即使在需要本地更改图标的情况下,例如调整窗口大小或悬停在本机按钮上时,它仍然保持Qt :: ArrowCursor。
如何在未悬停在图标上时强制光标正常工作?
以下是我的操作:
bool MiniItemDelegate::editorEvent(QEvent *event, QAbstractItemModel *model,
const QStyleOptionViewItem &option,
const QModelIndex &index)
{
// Emit a signal when the icon is clicked
QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
if(!index.parent().isValid() &&
event->type() == QEvent::MouseMove)
{
QSize iconsize = m_iconAdd.actualSize(option.decorationSize);
QRect closeButtonRect = m_iconAdd.pixmap(iconsize.width(),iconsize.height()).
rect().translated(AddIconPos(option));
QSize iconRemoveSize = m_iconRemove.actualSize(option.decorationSize);
QRect iconRemoveRect = m_iconRemove.pixmap(iconRemoveSize.width(),iconRemoveSize.height()).
rect().translated(RemoveIconPos(option));
if(closeButtonRect.contains(mouseEvent->pos()))
{
QApplication::setOverrideCursor(QCursor(Qt::PointingHandCursor));
}
else if(iconRemoveRect.contains(mouseEvent->pos()))
{
QApplication::setOverrideCursor(QCursor(Qt::PointingHandCursor));
}
else
{
Qt::CursorShape shape = Qt::ArrowCursor;
QApplication::setOverrideCursor(QCursor(shape));
}
}
if(!index.parent().isValid() &&
event->type() == QEvent::MouseButtonRelease)
{
QSize iconsize = m_iconAdd.actualSize(option.decorationSize);
QRect closeButtonRect = m_iconAdd.pixmap(iconsize.width(),iconsize.height()).
rect().translated(AddIconPos(option));
QSize iconRemoveSize = m_iconRemove.actualSize(option.decorationSize);
QRect iconRemoveRect = m_iconRemove.pixmap(iconRemoveSize.width(),iconRemoveSize.height()).
rect().translated(RemoveIconPos(option));
if(closeButtonRect.contains(mouseEvent->pos()))
{
;
}
else if(iconRemoveRect.contains(mouseEvent->pos()))
{
;
}
}
return false;
}