Note that there are some explanatory texts on larger screens.

plurals
  1. POQML callback from C++ with custom type as parameter
    primarykey
    data
    text
    <p>I would like to be able to call a QML function from C++ with an instance of a custom class as a parameter and then manipulate the instance from QML.</p> <p>Here is what I did so far :</p> <p>Data.h</p> <pre><code>class Data : public QObject { Q_OBJECT Q_PROPERTY(QString text READ text WRITE setText) public : Data() : QObject(), _text("Foo") { } virtual ~Data() { } Data(const Data &amp; other) { _text = other._text; } QString text() const { return _text; } void setText(const QString &amp; text) { _text = text; } private : QString _text; }; Q_DECLARE_METATYPE(Data); </code></pre> <p>Main.cpp</p> <pre><code>#include "Data.h" int main(int argc, char *argv[]) { QApplication app(argc, argv); Data callBackData; QQmlEngine engine; QQmlComponent rootComponent(&amp;engine, QUrl::fromLocalFile("CallBack.qml")); QObject * rootObj = rootComponent.create(); QMetaObject::invokeMethod(rootObj, "callMeBack", Q_ARG(QVariant, QVariant::fromValue(callBackData))); return app.exec(); } </code></pre> <p>CallBack.qml</p> <pre><code>import QtQuick 2.0 Item { function callMeBack(data) { console.log(data.text) } } </code></pre> <p>The console outputs <code>"Undefined"</code>. Did I do something wrong ?</p> <p>When changing the function body to <code>console.log(data)</code> it outputs <code>"QVariant(Data)"</code> so why can't I access the text property of data ?</p> <p>I tried registering <code>Data</code> as a QML type using <code>qmlRegisterType&lt;Data&gt;();</code> but this does not change anything.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload