QEvent in qml



  • Is there a way to pass a QEvent, QKeyEvent or QMouseEvent from c++ to qml without writing my own wrapper classes?



  • You will need to wrap them in some fashion (you can see the wrappers used by the built-in elements at src/declarative/graphicsitems/qdeclarativeevents_p.h).

    Regards,
    Michael



  • Thanks Michael

    I implemented wrappers similar to the ones in qdeclarativeevents_p.h but when I try to use them in my qml I get this error:

    QMetaProperty::read: Unable to handle unregistered datatype 'QMLKeyEvent*' for property 'QDeclarativeBoundSignalParameters::event'

    I register the QMLKeyEvent class with this call:

    @
    qmlRegisterType<QMLKeyEvent>();
    @

    And at the end of my header file that contains the QMLKeyEvent class I put:

    @
    QML_DECLARE_TYPE(QMLKeyEvent)
    @

    Is there something else I need to do to register this type?



  • I can't think of anything else you'd need to do.

    What version of Qt are you running (there have been a lot of bug fixes since 4.7.0)? Can you share what your QML looks like?

    Regards,
    Michael



  • I believe I'm using Qt 4.7.1.

    My QML code looks like this:

    @
    UserEventFilter {
    id: userEventFilter;

    onKeyPressed: {
    console.log(event.text);
    }
    }
    @

    UserEventFilter code looks like this:

    @
    bool UserEventFilter::processKeyPress(QEvent *event) {
    QMLKeyEvent ke(*static_cast<QKeyEvent *>(event));
    ke.setAccepted(false);

    emit keyPressed(&ke);
    emit userEventOccurred();

    return ke.isAccepted();
    }
    @

    I'm going to upgrade to Qt 4.7.2 and see what happens.



  • Are there any examples out there that show how to emit a signal with an argument that is not a basic type?
    Also, are there examples of emitting a signal with an argument that is not read only?



  • It's working... I think I had something wrong with my build.


Log in to reply
 

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