Do not use links to pass values ββfrom the QML world to the CPP world. This simple example works:
test.h
#ifndef TEST_H #define TEST_H #include <QObject> #include <QDebug> #include <QVariantMap> class Test : public QObject { Q_OBJECT public: Test(){} Q_INVOKABLE bool generate(QVariantMap context) {qDebug() << context;} }; #endif // TEST_H
main.cpp
#include <QGuiApplication> #include <QQmlApplicationEngine> #include <QQmlContext> #include "test.h" int main(int argc, char *argv[]) { QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QGuiApplication app(argc, argv); QQmlApplicationEngine engine; engine.rootContext()->setContextProperty(QStringLiteral("Test"), new Test()); engine.load(QUrl(QLatin1String("qrc:/main.qml"))); if (engine.rootObjects().isEmpty()) return -1; return app.exec(); }
main.qml
import QtQuick 2.7 import QtQuick.Controls 2.0 import QtQuick.Layouts 1.3 ApplicationWindow { visible: true width: 640 height: 480 title: qsTr("Hello World") MouseArea { anchors.fill: parent onClicked: { Test.generate({foo: "bar"}); } } }
click in the window, it will print the following messages in the output console:
QMap(("foo", QVariant(QString, "bar")))
source share