在这种情况下,SetContextProperty()如何失败

How does setContextProperty() fail in this situation?

本文关键字:何失败 失败 这种情况下 SetContextProperty      更新时间:2023-10-16

我在C 中创建了三个类,作为窗口,PropertyList和MyParams,我想将这两个类传递给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);
}

为什么会起作用:

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

此失败:

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

是因为我将PropertyList声明为q_object?我该如何解决?非常感谢。

propertylist.test((可以成功地称为myparams.test((,无法调用并崩溃qml。

我在您的代码示例中看不到任何问题,因为示例还不完整(请考虑:https://stackoverflow.com/help/reprex(。

尽管如此,我还是为您实施了解决方案。我希望它会有所帮助。(警告Window类将有内存泄漏,但应该理解C 和QML绑定(

这是您的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

这是您的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

这是您的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();
}

这是您的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()
            }
        }
    }
}