Change Mouse Cursor



  • I'm trying to change the mouse cursor from QML.

    Update: Cleared the content of this post by miss clicking. Sorry about that.



  • Hi,

    That's correct, there isn't currently a cursor property (this is tracked by "QTBUG-10642":http://bugreports.qt.nokia.com/browse/QTBUG-10642).

    In the above, I believe "this" actually refers to the global object -- try giving the rectangle an id and passing that to your function instead.

    Regards,
    Michael



  • Hello mbrasser,

    Updated my code to this:
    @Rectangle {
    id: toto
    Component.onCompleted:
    {
    wView.setItemCursor(toto);
    }
    }@

    It appears I'm still getting an invalid pointer. I also tried changing my function prototype to QDeclarativeItem but that does not work either.

    Any idea ?

    Thanks.

    B.A.



  • What version of Qt are you using? There was a bug at one point with QObject-derived function parameters, which should now be fixed (though I'm not sure if the fix was in 4.7.1 or coming in 4.7.2). If this is the issue you are running into, changing the function declaration to take a QObject* may work around it for you.

    Regards,
    Michael



  • Sorry, I am a newbie to qt. I am trying to change the mouse cursor, from the point cursor to stretch cursor, when the mouse is over a MouseArea. Is this possible? I have read the above posts, but I don't understand what is going on,

    Many thanks.



  • Hi Franziss,

    This is currently not possible with the QML MouseArea.

    You have to declare a QML QObject class with this function:

    @/* Q_INVOKABLE */ void WControllerView::setItemCursor(QGraphicsObject * object, const QString & shape)
    {
    if (object == NULL)
    {
    qWarning("WControllerView::setItemCursor invalid QGraphicsObject");
    return;
    }

    if (shape == "")
    {
        object->unsetCursor();
        return;
    }
    
    Qt::CursorShape cursor;
    
    if      (shape == "PointingHandCursor") cursor = Qt::PointingHandCursor;
    else if (shape == "ArrowCursor")        cursor = Qt::ArrowCursor;
    else if (shape == "IBeamCursor")        cursor = Qt::IBeamCursor;
    else if (shape == "BlankCursor")        cursor = Qt::BlankCursor;
    

    // Add your "stretch cursor" here
    else cursor = Qt::ArrowCursor;

    object->setCursor(cursor);
    

    }@

    Then call it from QML like this:

    @Rectangle {
    id: toto
    Component.onCompleted:
    {
    wView.setItemCursor(toto, "YourCursorShape");
    }
    }@



  • Hi Bunjee,

    Thank you very much for your code, it works! But I use slots instead of Q_INVOKABLE

    Have a nice day!



  • Hi All
    Many thanks for good idea.
    But I try use this solution fot ListView delegate and got app crash. Have you any idea?
    Sorry I newbie in Qt & QML



  • You could use the example code here:

    http://developer.qt.nokia.com/doc/qt-4.7/qml-mousearea.html

    The Qt Quick Desktop components offer a very similar class, too.

    Nils



  • Many thanks Nils!
    Very good and clear example


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.