set cursor shape from c++ backend
Solved
QML and Qt Quick
-
wrote on 17 Jul 2018, 18:42 last edited by
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 theBackEnd
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)
andQt::CursorShape(BlankCursor)
in the debug output
Any tips on how to hide/show the cursor from the c++ backend ?
- note: I see
-
-
wrote on 18 Jul 2018, 14:10 last edited by
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)) }
-
wrote on 18 Jul 2018, 14:14 last edited by
turns out I don't need to QCursor object, QCursor::setPos(x,y) works too
1/4