Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. set cursor shape from c++ backend
Forum Update on Monday, May 27th 2025

set cursor shape from c++ backend

Scheduled Pinned Locked Moved Solved QML and Qt Quick
4 Posts 2 Posters 1.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    rghvdberg
    wrote on 17 Jul 2018, 18:42 last edited by
    #1

    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 ?

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 17 Jul 2018, 19:59 last edited by
      #2

      Hi,

      You are likely looking for QGuiApplication::setOverrideCursor.

      Hope it helps

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      4
      • R Offline
        R Offline
        rghvdberg
        wrote on 18 Jul 2018, 14:10 last edited by
        #3

        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))
        
                }
        
        1 Reply Last reply
        1
        • R Offline
          R Offline
          rghvdberg
          wrote on 18 Jul 2018, 14:14 last edited by
          #4

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

          1 Reply Last reply
          0

          1/4

          17 Jul 2018, 18:42

          • Login

          • Login or register to search.
          1 out of 4
          • First post
            1/4
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved