stephen
stephen

Reputation: 71

How does setContextProperty() fail in this situation?

I have got three classes created in C++ as Window, PropertyList and MyParams and I want to pass PropertyList and MyParams these two classes to qml.

class Window
{
public:
    PropertyList* getPropertyList();
private:
    PropertyList* propertyList;
};

class PropertyList : public QObject
{
    Q_OBJECT
public:
    MyParams* getMyParams();
    Q_INVOKABLE void test();

private:
    MyParams* myParams;
};

class MyParams : public QObject
{
    Q_OBJECT
public:
    Q_INVOKABLE void test();
};

int main(int argc, char *argv[])
{
    Window* window = new Window();
    PropertyList* propertyList = window->getPropertyList();
    MyParams* myParam = propertyList->getMyParams();

    QQmlApplicationEngine engine;
    engine.rootContext()->setContextProperty(QStringLiteral("myParams"), myParam);

    engine.rootContext()->setContextProperty(QStringLiteral("propertyList"), propertyList);
}

Why does this work:

engine.rootContext()->setContextProperty(QStringLiteral("propertyList"), propertyList);

And this fail:

 engine.rootContext()->setContextProperty(QStringLiteral("myParams"), myParam);

Is it because I declare PropertyList as Q_OBJECT? And how can I fix this? Thanks a lot.

propertyList.test() can be called succesfully while myParams.test() cannot be called and crashed the qml.

Upvotes: 1

Views: 983

Answers (1)

Hubi
Hubi

Reputation: 1639

I cannot see any problems in your code example, because the sample is not complete (Consider: https://stackoverflow.com/help/reprex).

Nevertheless I implemented a solution for you. I hope it will help. (Warning: there will be memory leaks around class Window, but it should helpt to understand C++ and Qml binding)

Here is your PropertyList.h:

#ifndef PROPERTYLIST_H
#define PROPERTYLIST_H

#include "myparams.h"

#include <QObject>

class PropertyList : public QObject
{
    Q_OBJECT
public:
    explicit PropertyList(QObject *parent = nullptr) : QObject (parent) { }

    MyParams* getMyParams()
    {
        return  myParams;
    }

    Q_INVOKABLE void test()
    {
        qDebug() << "PropertyList test";
    }

private:
    MyParams* myParams = new MyParams();
};

#endif // PROPERTYLIST_H

Here is your MyParams.h:

#ifndef MYPARAMS_H
#define MYPARAMS_H

#include <QObject>
#include <QDebug>

class MyParams : public QObject
{
    Q_OBJECT
public:
    explicit MyParams(QObject *parent = nullptr) : QObject (parent) { }

    Q_INVOKABLE void test()
    {
        qDebug() << "MyParams test";
    }
};

#endif // MYPARAMS_H

Here is your main.cpp:

#include "propertylist.h"

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlEngine>
#include <QQmlContext>
#include <QDebug>

class Window
{
public:
    PropertyList* getPropertyList()
    {
        return propertyList;
    }

private:
    PropertyList* propertyList = new PropertyList(nullptr);
};

int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QGuiApplication app(argc, argv);

    Window* window = new Window();
    PropertyList* propertyList = window->getPropertyList();
    MyParams* myParam = propertyList->getMyParams();

    QQmlApplicationEngine engine;
    engine.rootContext()->setContextProperty(QStringLiteral("myParams"), myParam);

    engine.rootContext()->setContextProperty(QStringLiteral("propertyList"), propertyList);
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    if (engine.rootObjects().isEmpty())
    {
            return -1;
    }

    return app.exec();
}

Here is your main.qml:

import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.0

Window {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")

    ColumnLayout {
        anchors.centerIn: parent
        Button {
            text: "myParams"
            onClicked: {
                myParams.test()
            }
        }

        Button {
            text: "propertyList"
            onClicked: {
                propertyList.test()
            }
        }
    }
}

Upvotes: 3

Related Questions