gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 03:16:33 24 4
gpt4 key购买 nike

我在 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com