How to change the cursor shape



  • Hello,
    I extend a class from QQuickPaintedItem and implement virtual function paint. Then I want to change mouse cursor to Qt::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.


  • Moderators

    @webberg
    You should rather use QWidget::setCursor() than cursor().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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.