JustWe
JustWe

Reputation: 4484

QML: Fetch mouse event of MouseArea in C++

I am trying to connect MouseArea mouse event with C++, But the QQuickMouseArea is private, so i could not fetch the signal.

Like this:

QObject::connect(mouseAreaObj, SIGNAL(released(QMouseEvent*)),
                 handlerObj, SLOT(handleEvent(QMouseEvent*)));

Is there any way to solve this?

And if not able, i wonder why Qt not allow us to access QQuickMouseArea.

Upvotes: 4

Views: 2433

Answers (2)

tauran
tauran

Reputation: 8036

I made the connection in qml mouseArea.clicked.connect(cppObject.onClicked) and then in C++ simply received a QObject* which has properties as expected:

void CppClass::onClicked(QObject *event) {
    qDebug() << "clicked" << event->property("x").toInt();
}

Upvotes: 2

GrecKo
GrecKo

Reputation: 7150

Instead of listening to QQuickMouseArea signals, you can get it as a QObject and set your handlerObj as an event filter of your mouseAreaObj like this : mouseAreaObj->installEventFilter(handlerObj).

Then you'll need to implement eventFilter in your handlerObj. Maybe something like this :

bool HandlerObject::eventFilter(QObject* obj, QEvent* event)
{
    if (event->type() == QEvent::MouseButtonRelease)
        return handleEvent(static_cast<QMouseEvent*>(event));
    else
        return false;
}

Upvotes: 4

Related Questions