c++ - setContextProperty() 在这种情况下如何失败?

标签 c++ qt qml

我在 C++ 中创建了三个类,分别是 Window、PropertyList 和 MyParams,我想将 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 绑定(bind))

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

关于c++ - setContextProperty() 在这种情况下如何失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56254983/

相关文章:

c++ - OpenALPR 退出并出现带有自定义训练数据的段错误

c++ - Qt:如果你发送信号太快会发生什么?

python - 在 PyQt 中创建和添加 MapQuickItem 到 map

c++ - QML TableView 使用 QtQuick.Controls 2 单击行

c++ - 未定义的引用链接 yaml-cpp 程序与 mingw-w64 + cmake

java - Android ndk log unicode(宽字符)

c++ - 如何正确中止 Qt 中的网络请求?

qt - QML 虚拟键盘隐藏按钮不起作用

c++ - 如何在 MFC Dialog 应用程序中插入更多对话框?

qt - 如何使 QWidget alpha 透明