How to change the cursor shape
-
Hello,
I extend a class fromQQuickPaintedItem
and implement virtual functionpaint
. Then I want to change mouse cursor toQt::IBeamCursor
when mouse move into text area where I draw. I wrote the following function to reply mouse move event:Q_INVOKABLE void MyView::mouseMove(int x, int y) { if(IsInTextArea() && cursor().shape()!=Qt::IBeamCursor){ cursor().setShape(Qt::IBeamCursor); return; } if(!IsInTextArea() && cursor().shape()!= Qt::ArrowCursor){ cursor().setShape(Qt::ArrowCursor); return; } }
I detect it has called
cursor().setShape(Qt::IBeamCursor);
but nothing changed. So what's wrong with my code? thank you. -
Hello,
I extend a class fromQQuickPaintedItem
and implement virtual functionpaint
. Then I want to change mouse cursor toQt::IBeamCursor
when mouse move into text area where I draw. I wrote the following function to reply mouse move event:Q_INVOKABLE void MyView::mouseMove(int x, int y) { if(IsInTextArea() && cursor().shape()!=Qt::IBeamCursor){ cursor().setShape(Qt::IBeamCursor); return; } if(!IsInTextArea() && cursor().shape()!= Qt::ArrowCursor){ cursor().setShape(Qt::ArrowCursor); return; } }
I detect it has called
cursor().setShape(Qt::IBeamCursor);
but nothing changed. So what's wrong with my code? thank you.@webberg
You should rather useQWidget::setCursor()
thancursor().setShape()
:Q_INVOKABLE void MyView::mouseMove(int x, int y) { if(IsInTextArea() && cursor().shape()!=Qt::IBeamCursor){ this->setCursor(Qt::IBeamCursor); return; } if(!IsInTextArea() && cursor().shape()!= Qt::ArrowCursor){ this->setCursor(Qt::ArrowCursor); return; } }
-
@webberg
You should rather useQWidget::setCursor()
thancursor().setShape()
:Q_INVOKABLE void MyView::mouseMove(int x, int y) { if(IsInTextArea() && cursor().shape()!=Qt::IBeamCursor){ this->setCursor(Qt::IBeamCursor); return; } if(!IsInTextArea() && cursor().shape()!= Qt::ArrowCursor){ this->setCursor(Qt::ArrowCursor); return; } }
@raven-worx Thanks! I found I use MouseArea in my view. I changed the cursor of my view. But in fact, I should change the cursor shape of MouseArea in QML.