Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QML: Fetch mouse event of MouseArea in C++

Tags:

c++

qt

qml

qtquick2

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.

like image 413
JustWe Avatar asked Oct 30 '25 03:10

JustWe


2 Answers

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;
}
like image 167
GrecKo Avatar answered Oct 31 '25 18:10

GrecKo


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();
}
like image 25
tauran Avatar answered Oct 31 '25 16:10

tauran



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!