Send array from QML to c++ via signal
-
Hi,
I want to pass an array from QML to C++ via a signal:
signal passArray(variant array) function emitPassArray() { var i var array = [] for(i=0; i<gridView.model.count;i++) { var myob =gridView.model.items.get(i).model array.push(myob.property1) } passArray(array) }
and I thought I catch it on the c++ side with a
void getArray(QVariantList array);
slot. However, I get the message, that it is not possible to connect
getArray
as there is no signal passArray(QVariantList). What am I doing wrong? -
You can expose qlist as qproperty from c++ and set the list in qml. No need pass signal and catch it. Is there a hard requirement to send signal only ?
-
@maxwell31 said in Send array from QML to c++ via signal:
However, I get the message, that it is not possible to connect getArray as there is no signal passArray(QVariantList). What am I doing wrong?
check the output of the following:
const QMetaObject* mo = myQmlObject->metaObject(); for (int i = mo->methodOffset(); i < mo->propertyCount(); ++i) { QMetaMethod m = mo->method(i); if( m.methodType() != QMetaMethod::Signal ) continue; qDebug() << m.methodSignature(); }
This prints the signatures of all the object's signals.
I guess the signature is just wrong. Since you defined the signal in QML with a
variant
type, i think the correct parameter type will beQVariant
instead. -
@raven-worx said in Send array from QML to c++ via signal:
I guess the signature is just wrong. Since you defined the signal in QML with a
variant
type, i think the correct parameter type will beQVariant
instead.Indeed. After that, you can convert the data using
QVariant::toList()
.http://doc.qt.io/qt-5/qtqml-cppintegration-interactqmlfromcpp.html#connecting-to-qml-signals says "When a QML object type is used as a signal parameter, the parameter should use
var
as the type, and the value should be received in C++ using theQVariant
type"Also, according to http://doc.qt.io/qt-5/qml-variant.html,
variant
is "obsolete and exists only to support old applications; new applications should usevar
type properties instead."