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. Change mousecursor from QML with setContextProperty and Qt::CursorShape
Forum Updated to NodeBB v4.3 + New Features

Change mousecursor from QML with setContextProperty and Qt::CursorShape

Scheduled Pinned Locked Moved QML and Qt Quick
1 Posts 1 Posters 4.1k Views 1 Watching
  • 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.
  • M Offline
    M Offline
    markg85
    wrote on 1 Aug 2012, 13:15 last edited by
    #1

    Hi,

    This is not a request for help. Just a post showing people how to do this.
    My case was as follows. I needed to change my cursor shape to Qt::SizeVerCursor for some resizing QML component that i'm making. But i didn't want to make my own QML component just for that so i made a utils class which is assigned to QML by using setContextProperty. In that class i had a function with this:

    @Q_INVOKABLE void setCursor(Qt::CursorShape shape);@

    That however doesn't work when calling from QML. It does when you make a own custom component, but apparently it doesn't when you use setContextProperty. This is very annoying because all the Qt::CursorShape values are available in QML right now under Qt.<enum_name> so Qt.SizeVerCursor would work for me in passing the value like so:
    Note: <property> is the value you set when using setContextProperty.

    <property>.setCursor(Qt.SizeVerCursor);

    Sadly, the function argument to allow the type: Qt::CursorShape isn't registered and doesn't want to be registered with qmlRegisterType since it's not an object, just an enum...

    So i had the issue of not being able to pass Qt::CursorShape as a function argument when i'm using setContextProperty.
    Now i could go for this solution: http://qt-project.org/forums/viewthread/1886 (passing a string to the function) which would make the call something like this:

    <property>.setCursor("SizeVerCursor");

    That's obviously not what i wanted to do. Now here comes a neat trick. Enums are just int numbers so you can pass them in a function that accepts an int argument. The function look like this:

    @Q_INVOKABLE void setCursor(int shape);@

    Now in QML you can simply do:

    <property>.setCursor(Qt.SizeVerCursor);

    In the C++ side you can just check like this:

    @void ClassName::setCursor(int shape)
    {
    if(shape == Qt::SizeVerCursor)
    {
    // ... do something with it
    }
    }
    @

    This works and allows you to use the full enum of http://qt-project.org/doc/qt-4.8/qt.html#CursorShape-enum without the need to add some custom string parsing/checking.

    What i would've liked more is just the ability to register a function argument type. Apparently that's only possible when sending full blown objects to QML, not with setContextProperty ...

    This probably helps a few people out there :)
    Note: in QML 2 this is a lot easier if you need to change the cursor shape on some MouseArea. There is just a property for it in QML 2: http://doc-snapshot.qt-project.org/5.0/qml-qtquick2-mousearea.html#cursorShape-prop

    Cheers,
    Mark

    1 Reply Last reply
    0

    1/1

    1 Aug 2012, 13:15

    • Login

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