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. MouseArea cursorShape with a custom BitmapCursor

MouseArea cursorShape with a custom BitmapCursor

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
7 Posts 3 Posters 1.8k 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.
  • E Offline
    E Offline
    Emilie
    wrote on last edited by
    #1

    Is there any way to set a custom BitmapCursor to a MouseArea ?
    From the MouseArea doc only the predefined cursor shapes are available.
    Something like :

     MouseArea {
        anchors.fill: parent
        acceptedButtons: Qt.AllButtons
        hoverEnabled: true
        cursorShape: Qt.BitmapCursor("qrc:/skin/cursor_rotate")
    }
    
    1 Reply Last reply
    0
    • IntruderExcluderI Offline
      IntruderExcluderI Offline
      IntruderExcluder
      wrote on last edited by
      #2

      There is no built-in way to change cursor, but you can use quick-private module and override MouseArea. Add QT += quick-private to your *.pro file.
      Create CustomMouseArea class:

      #include <QPixmap>
      #include <QCursor>
      #include <QtQuick/private/qquickmousearea_p.h>
      
      class CustomMouseArea : public QQuickMouseArea
      {
          Q_OBJECT
      public:
          explicit CustomMouseArea(QQuickItem* parent = nullptr) : QQuickMouseArea(parent) {}
      
      public slots:
          void setBitmapCursor(const QString& path) {
              QPixmap p(path);
              QCursor c(p);
              setCursor(c);
          }
      };
      

      Register it as QML type in main.cpp before qml file loaded to engine:

      qmlRegisterType<CustomMouseArea>("Test", 1, 0, "CustomMouseArea");
      

      Use in in QML:

      ...
      import Test 1.0
      ...
          CustomMouseArea {
              anchors.fill: parent
      
              Component.onCompleted: setBitmapCursor(":/images/cur1.png")
          }
      

      This is just a simple examle, better way to use Q_PROPERTY.

      1 Reply Last reply
      1
      • E Offline
        E Offline
        Emilie
        wrote on last edited by
        #3

        Thanks a lot for your reply. It worked on a test qml project using .pro. But for my main project I'm using cmake, and I haven't found a proper way to have the header available..

        1 Reply Last reply
        0
        • IntruderExcluderI Offline
          IntruderExcluderI Offline
          IntruderExcluder
          wrote on last edited by
          #4

          Should be something like:

          include_directories (${Qt5Quick_PRIVATE_INCLUDE_DIRS})
          
          1 Reply Last reply
          0
          • GrecKoG Offline
            GrecKoG Offline
            GrecKo
            Qt Champions 2018
            wrote on last edited by GrecKo
            #5

            This can be done without private headers too, with a helper object as standalone or attached object just calling setCursor on the MouseArea.

            It could look like this on the QML Side

            MouseArea {
                Cursor.customPath: "qrc:/skin/cursor_rotate"
            }
            
            1 Reply Last reply
            0
            • E Offline
              E Offline
              Emilie
              wrote on last edited by
              #6

              Thanks @IntruderExcluder your solution is working fine. But of course I would rather not inherit a private class. @GrecKo can you be more precise ? I don't see how I could set a cursor in a MouseArea from an helperObject.. There is no Cursor property in the MouseArea so even with my helper object built (inheriting a QQuickItem) I don't know how to call it from within the mouseArea..

              1 Reply Last reply
              0
              • IntruderExcluderI Offline
                IntruderExcluderI Offline
                IntruderExcluder
                wrote on last edited by IntruderExcluder
                #7

                As a superfast solution you can create a custom class which extends QQuickItem:

                class ParentCursorChanger : public QQuickItem
                {
                    Q_OBJECT
                public:
                    explicit ParentCursorChanger(QQuickItem* parent = nullptr) : QQuickItem(parent) {}
                
                public slots:
                    void setBitmapCursor(const QString& path) {
                        QPixmap p(path);
                        QCursor c(p);
                        qobject_cast<QQuickItem*>(parent())->setCursor(c);
                    }
                };
                

                Register it the same way as I posted before and do something like this in QML:

                MouseArea {
                    anchors.fill: parent
                    
                   CursorChanger {
                        Component.onCompleted: setBitmapCursor(":/images/cur1.png")
                    }
                }
                

                But the super true way would be using attached properties ofcourse.

                1 Reply Last reply
                0

                • Login

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