我试图将Object从qml传递到c++作为slot的参数,是否可以进入c++?在c++的槽中,我有一个空变量QVariant(QJSValue,(。我需要将Object作为映射传递给c++(QMap<QString,QString>(。有可能通过这种方式传递物体吗?
一些.h
class Some : public QObject
{
Q_OBJECT
public slot:
Q_INVOKABLE void someSlot(QVariant variableToPass);
}
Some.cpp
void Some :: someSlot(QVariant variableToPass){
qDebug()<<"Object from qml"<< variableToPass; // QVariant(QJSValue, ) empty variable!!!!!!!!!!!!!!
qDebug()<<"variableToPass.canConvert<QJSValue>()"<<variableToPass.canConvert<QJSValue>();//true, but empty!!!
QJSValue jsValue = variableToPass.value<QJSValue>();
QMap<QString,QString> data = jsValue.toVariant().value<QMap<QString,QString>>();
}
some2.qml
Item {
Button {
onClicked: registeredInQmlVar.someSlot({"Login":"Cash","Password":"123"})//call slot and pass Object with two parameters
}
}
我的错误是在传递对象的键中加引号,并用QMap<Q字符串,QVariant>data=variableToPass.toMap((;