Important: Please read the Qt Code of Conduct -

Can qml signal parameter MouseEvent be mapped to C++ class?

  • I've a qml signal with MouseEvent as parameter, for example, 'clicked(var mouse)',and mouse is passed from MouseArea::onClicked(MouseEvent mouse).
    I tried to connect this signal to some slot in c++ like this,
    @connect(qmlOblect, SIGNAL(clicked(QMouseEvent*)),SLOT(onClicked(QMouseEvent*)));@

    When linking, qtcreator complains that No such signal in qmlObj.
    I wonder whether qml MouseEvent can be mapped to c++ class QmouseEvent, or perhaps I missed something here?

  • Moderators


    MouseEvent from QML is QQuickMouseEvent, not QMouseEvent. It's a private class though, and I'm not sure if it's meant to be used in C++.

    What are you trying to do?

  • I want to connect qml mouseevent signal to c++ slot in my c++ code. so that i can process logical staff related to qml mouse event in c++.
    Since QMouseEvent is not correct and QQuickMouseEvent is private, I've to define a new QObject-derived class samiliar to QMouseEvent/QQuickMouseEvent and register it to qml context. Is this right?

  • I don't think that will work if you use a custom event class.
    Why not use the arguments you need from the mouse event and only pass those to the c++ slot? e.g. only the position x/y as qreal or whatever you need from the event.

  • @Xander84
    Yes, you're right. I've done like what you suggested. It works. Thank u.

Log in to reply