编辑 问题已解决。在下面查看我的编辑
我在使用 QQmlContext::setContextObject 使 C++ 对象对 QML 可见时遇到问题。我已经在 link 阅读了 QQmlContext 的文档。 ,这表明我可以使用 setContextObject 使 QObject 派生类的 Q_PROPERTY 对 QML 可见。下面的代码说明了这个问题。
main.cpp
#include <QObject>
#include <QQmlEngine>
#include <QGuiApplication>
class MyClass : public QObject
{
Q_OBJECT
Q_PROPERTY(QString myProperty READ prop NOTIFY propChanged)
public:
MyClass(QObject * parent = 0) : QObject(parent) {}
QString prop() { return QString("Hello from MyClass"); }
Q_SIGNALS:
void propChanged(void);
};
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQmlEngine engine;
QQmlContext *objectContext = new QQmlContext(engine.rootContext());
MyClass myClass;
objectContext->setContextObject(&myClass);
QQmlComponent component(&engine, "main.qml");
QObject *object = component.create(objectContext);
return app.exec();
}
main.qml
import QtQuick 2.1
import QtQuick.Controls 1.0
ApplicationWindow
{
Text
{
text: myProperty
}
}
当我运行这个程序时出现错误
file:///C:/Path/to/main.qml:8: ReferenceError: myProperty is not defined
提前感谢您的帮助。
环境。我在 Windows 7 上使用 Qt 5.1.1,带有 MSVC2010 编译器
编辑 回答我自己的问题。干净的重建显示我的构建输出文件夹中显然有一些过时的对象。
注意一点:MyClass 必须在一个单独的文件中,否则 moc 编译器无法发挥它的魔力。
我整理后的 main.cpp 现在看起来像这样
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQmlEngine engine;
QQmlContext * context = new QQmlContext(engine.rootContext());
QObject::connect(&engine, SIGNAL(quit()), QCoreApplication::instance(), SLOT(quit ()));
MyClass myClass;
context->setContextObject(&myClass);
QQmlComponent component(&engine, "main.qml");
QQuickWindow * topLevel = qobject_cast<QQuickWindow*>(component.create(context));
topLevel->show();
int rc = app.exec();
delete topLevel;
delete context;
return rc;
}
最佳答案
您可以尝试在 getter 函数声明中添加 Q_INVOKABLE
宏。如果它没有帮助,您可以考虑使用 QQmlContext::setContextProperty
来执行此操作。我从未见过有人使用 ::setContextObject
进行这种集成。
关于c++ - 使用 QQmlContext::setContextObject 使 C++ 对象对 QML 可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19516851/