ddyn
ddyn

Reputation: 55

Sending JavaScript Object QML signal parameter

I am trying to connect a QML signal to a Qt slot with following parameter types:

in QML side:

signal Sig(var info)

in Qt side:

QObject::connect(topLevel, SIGNAL(Sig(QVariantMap)), &mObj, SLOT(mSlot(QVariantMap)));

Which gives me the following:

QObject::connect: No such signal QQuickWindowQmlImpl_QML_24::Sig(QVariantMap) in ...

So, I assume that types var and QVariantMap does not match. According to this document, QVariantMap types are converted to JavaScript objects. But I am not sure if it also does the other way around.

I have implemented an opposite type of connection(Qt signal with QVariantMap, QML handler with "Connections" element) which worked just fine. I was able to get the signal's argument as a JS object.

By the way, I have also tried the same connection with string argument types in my code, so I don't think that there is another unrelated mistake in my code.

How do I pass JS objects to Qt(C++) side using signal/slot mechanism? I haven't been able to find a solution. Not even an example that matches my case(signal and slot argument types), actually. Which makes me think that I am doing some sort of design mistake.

Upvotes: 0

Views: 487

Answers (1)

hyde
hyde

Reputation: 62797

The parameters are QVariants on C++ side, so you need to do

QObject::connect(topLevel, SIGNAL(Sig(QVariant)), &mObj, SLOT(mSlot(QVariant)));

Note that you also need to change mSlot parameter type, because QVariant can't be implicitly converted to QVariantMap. To get the map in the slot, use QVariant::toMap() method, if it indeed is a map (if it isn't, this method returns empty map, and you need to do some debugging).

It works the other way around, because QVariantMap can be implicitly converted to QVariant.

Upvotes: 1

Related Questions