Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved Position mouse cursor

    QML and Qt Quick
    3
    9
    9529
    Loading More Posts
    • 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
      rghvdberg last edited by

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

      1 Reply Last reply Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        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 Reply Quote 0
        • R
          rghvdberg last edited by

          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 Reply Quote 0
          • SGaist
            SGaist Lifetime Qt Champion last edited by

            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 Reply Quote 1
            • R
              rghvdberg @SGaist last edited by

              @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 Reply Quote 1
              • R
                rghvdberg last edited by

                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 Reply Quote 1
                • R
                  rghvdberg last edited by

                  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 Reply Quote 2
                  • R
                    rghvdberg last edited by

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

                    1 Reply Last reply Reply Quote 2
                    • E
                      erika_rafaesky last edited by

                      This post is deleted!
                      1 Reply Last reply Reply Quote 0
                      • First post
                        Last post