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. Position mouse cursor

Position mouse cursor

Scheduled Pinned Locked Moved Solved QML and Qt Quick
9 Posts 3 Posters 12.5k 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 last edited by
    #1

    Is it possible to move the mouse position?
    Something like setPos(x, y)

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Out of curiosity, what is your purpose for that ?

      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
      0
      • R Offline
        R Offline
        rghvdberg
        wrote on last edited by
        #3

        The designer of the app wants the mouse pointer hidden when moving a control (knob or slider) and when the mouse is released the cursor should re appear at the stop position of the control. Even if it has been moved outside of the control.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          You can use the QCursor for that. You can take a look at this stack overflow answer to get a starting point.

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

          R 1 Reply Last reply
          1
          • SGaistS SGaist

            You can use the QCursor for that. You can take a look at this stack overflow answer to get a starting point.

            R Offline
            R Offline
            rghvdberg
            wrote on last edited by
            #5

            @SGaist thanks, I got it working, kind off
            as in the post you linked I need to figure out the x,y coordinates to jump to :-)

            1 Reply Last reply
            1
            • R Offline
              R Offline
              rghvdberg
              wrote on last edited by
              #6

              when I have a 'complete' solution I'll post the code here, in the case somebody needs to do the same

              1 Reply Last reply
              1
              • R Offline
                R Offline
                rghvdberg
                wrote on last edited by
                #7

                Solved it \o/

                Here's a (bit) shortened version of my code.

                • there's some qml code to hide the cursor when moving the slider, I think I'm gonna move that to c++ too.
                • according to the docs mapToGlobal(,x,y) returns a qml point a "x,y"
                  I logged the output of mapToGlobal() and it gave : QPointF(951, 519)
                  QtCursor::setPos() can't handle the QPointF , only QPoint, so I took the x and y of that point and used QtCursor::setPos(x,y).

                Comments welcome :-)

                C++ part

                ///backend.h
                #ifndef BACKEND_H
                #define BACKEND_H
                
                #include <QObject>
                #include <QCursor>
                
                class BackEnd : public QObject
                {
                    Q_OBJECT
                
                public:
                    explicit BackEnd(QObject *parent = nullptr);
                
                signals:
                
                public slots:
                    void moveCursor(QPointF p);
                
                private:
                    QCursor cursor;
                };
                
                #endif // BACKEND_H
                
                // backend.cpp
                #include "backend.h"
                
                BackEnd::BackEnd(QObject *parent) :
                    QObject(parent)
                {
                }
                
                void BackEnd::moveCursor(QPointF p)
                {
                    int x = p.x();
                    int y = p.y();
                    cursor.setPos(x,y);
                }
                
                main.cpp
                #include <QGuiApplication>
                #include <QQmlApplicationEngine>
                #include "backend.h"
                
                int main(int argc, char *argv[])
                {
                    QGuiApplication app(argc, argv);
                
                    qmlRegisterType<BackEnd>("borealis.backend", 1, 0, "BackEnd");
                
                    QQmlApplicationEngine engine;
                    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
                    if (engine.rootObjects().isEmpty())
                        return -1;
                    return app.exec();
                }
                

                QML part

                // main.qml
                
                import QtQuick 2.2
                import QtQuick.Window 2.2
                import borealis.backend 1.0
                import "components" // Slider.qml is in dir ./components
                
                ApplicationWindow {
                    id: borealis
                    width: 640
                    height: 480
                    visible: true
                    property alias backend: backend
                    title: qsTr("Borealis")
                    color: "#3b3b3b"
                
                    BackEnd {
                        id: backend // this links to the cpp code
                    }
                 
                  Slider{
                        anchors.horizontalCenter: parent.horizontalCenter
                        anchors.verticalCenter: parent.verticalCenter
                    }
                }
                
                // Slider.qml , custom slider (not using qtquick controls)
                Item {
                    id: root
                    property real value: 0.5
                    property int minimumValue: 0
                    property int maximumValue: 1
                    implicitWidth: 20
                    implicitHeight: 100
                    property variant mappedcursor
                
                    Rectangle {
                        id: back
                        height: root.height
                        width: root.width
                        color: "slate grey"
                    }
                    Rectangle {
                        id: slider
                        height: root.height * value
                        width: root.width
                        anchors.bottom: back.bottom
                        color: "dark grey"
                        anchors.bottomMargin: 0
                    }
                    Rectangle {
                        id: handle
                        height: 2
                        anchors.top: slider.top
                        anchors.topMargin: 0
                        width: root.width
                    }
                
                    MouseArea {
                        anchors.fill: root
                        cursorShape: pressed ? Qt.BlankCursor : Qt.ArrowCursor
                        onReleased: move_Cursor()
                        onMouseYChanged: {
                            var pos = maximumValue - mouse.y / root.height * (maximumValue - minimumValue) + minimumValue
                            root.value = Math.max(minimumValue, Math.min(pos, maximumValue))
                        }
                    }
                
                    function move_Cursor()
                    {
                        mappedcursor = mapToGlobal(handle.width / 2,handle.y)
                        backend.moveCursor(mappedcursor)
                    }
                }
                
                
                1 Reply Last reply
                2
                • R Offline
                  R Offline
                  rghvdberg
                  wrote on last edited by
                  #8

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

                  1 Reply Last reply
                  2
                  • E Offline
                    E Offline
                    erika_rafaesky
                    wrote on last edited by
                    #9
                    This post is deleted!
                    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