Reputation: 30895
i have item delegate that when the mouse event is over icon i change its Cursor to Qt::PointingHandCursor
when its off i set it back to Qt::ArrowCursor . its working fine .
the problem is that besides when it over the icon . it allways stack on Qt::ArrowCursor
even in situation when the icon needs to changes natively like when resizing the windows or when over native push button . it always Qt::ArrowCursor.
how can i force the Cursor act normally when its is no over the icon?
here is what i do :
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;
}
Upvotes: 3
Views: 5219
Reputation: 2987
You need to use restoreOverrideCursor()
to undo each call to setOverrideCursor()
. From the documentation:
http://doc.qt.io/archives/qt-4.7/qapplication.html#setOverrideCursor
Application cursors are stored on an internal stack. setOverrideCursor() pushes the cursor onto the stack, and restoreOverrideCursor() pops the active cursor off the stack. changeOverrideCursor() changes the curently active application override cursor. Every setOverrideCursor() must eventually be followed by a corresponding restoreOverrideCursor(), otherwise the stack will never be emptied.
You'll have to figure out exactly how to make this work in your code (it's not exactly clear what behavior you want), but you could start by replacing that else
clause
{
Qt::CursorShape shape = Qt::ArrowCursor;
QApplication::setOverrideCursor(QCursor(shape));
}
with
{
QApplication::restoreOverrideCursor();
}
NOTE: In my applications, I've created an RAII-style class that is initialized with a Qt::CursorShape
, calls setOverrideCursor(...)
in its constructor, and calls restoreOverrideCursor()
in its destructor. In this way, you can set the cursor shape within a function and be certain that this change will be un-done automatically by the time that function ends. This may not work in all cases, but when it does, it makes things easy.
Upvotes: 6