Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

set cursor shape from c++ backend



  • I've managed to position the mouse pointer from the c++ backend (see "Position mouse cursor" ).
    I've tried simply adding this piece of code to the BackEnd class, but it has no effect when I call that function.

    void BackEnd::setCursorShape(Qt::CursorShape cursorShape)
    {
        cursor.setShape(cursorShape);
        qDebug()<< cursorShape;
    }
    
    • note: I see Qt::CursorShape(ArrowCursor) and Qt::CursorShape(BlankCursor) in the debug output

    Any tips on how to hide/show the cursor from the c++ backend ?


  • Lifetime Qt Champion

    Hi,

    You are likely looking for QGuiApplication::setOverrideCursor.

    Hope it helps



  • It works !
    code:

    //backend.h
    #ifndef BACKEND_H
    #define BACKEND_H
    
    
    #include <QObject>
    #include <QCursor>
    #include <QGuiApplication>
    
    class BackEnd : public QObject
    {
        Q_OBJECT
    
    public:
        explicit BackEnd(QObject *parent = nullptr);
    
    signals:
    
    public slots:
        void moveCursor(QPointF p);
        void setCursorShape(Qt::CursorShape shape);
    private:
        QCursor cursor;
    };
    
    // backend.cpp
    #include "backend.h"
    #include <QDebug>
    
    BackEnd::BackEnd(QObject *parent) :
        QObject(parent)
    {
    
    }
    
    void BackEnd::moveCursor(QPointF p)
    {
        int x = p.x();
        int y = p.y();
        cursor.setPos(x,y);
    }
    
    void BackEnd::setCursorShape(Qt::CursorShape shape)
    {
        switch(shape)
        {
        case Qt::CursorShape::BlankCursor:
        {
            QGuiApplication::setOverrideCursor(QCursor(shape));
            break;
        }
    
        case Qt::CursorShape::ArrowCursor:
            {
                QGuiApplication::restoreOverrideCursor();
                break;
            }
        default: QGuiApplication::restoreOverrideCursor();
        }
    }
    
    // Slider.qml
    
    /*
    slider implentation 
    Rectangles , etc etc etc
    */ 
    
    // important bit
     MouseArea {
            id: mouse_area
            anchors.fill: root
            onPressedChanged:
            {
               if (pressed)  {
                   backend.setCursorShape(Qt.BlankCursor)
               }
            }
            onReleased:
            {
                move_Cursor()
                backend.setCursorShape(Qt.ArrowCursor)
            }
            onMouseYChanged: {
                var pos = maximumValue - mouse.y / root.height * (maximumValue - minimumValue) + minimumValue
                root.value = Math.max(minimumValue, Math.min(pos, maximumValue))
    
            }
    


  • turns out I don't need to QCursor object, QCursor::setPos(x,y) works too


Log in to reply