#ifndef APPGLOBAL_H
#define APPGLOBAL_H
#include <QObject>
class AppGlobal : public QObject
{
Q_OBJECT
public:
AppGlobal(QObject *parent = nullptr);
//测试代码
Q_PROPERTY(QString test READ test WRITE setTest NOTIFY testChanged)
QString test()
{
return m_test;
}
public slots:
void setTest(QString test)
{
if(m_test == test)
return;
m_test = test;
emit testChanged(m_test);
}
signals:
void testChanged(QString test);
private:
QString m_test = "test"; //测试代码
};
#endif // APPGLOBAL_H
#include "appglobal.h"
AppGlobal::AppGlobal(QObject *parent)
: QObject(parent)
{
}
AppGlobal appGlobal;
QQmlApplicationEngine engine;
engine.rootContext()->setContextProperty("$app", &appGlobal);
Text {
x: 0
y: 0
width: 200
height: 50
text: $app.test;
z:99
}
总结,用暴露法比较好用,对我们这种平民百姓是友好的