方法一:在QML系统中注册C++类型
----------------main.cc中:
//参数:qmlRegisterType<C++类型名> (命名空间 主版本 次版本 QML中的类型名)
qmlRegisterType<MyConfiguration, 1>("MyConfiguration", 1, 0, "MyConfiguration");
---------------类文件中:
/ 使用 Q_INVOKABLE 宏修饰的方法才可以在 QML 中被调用
Q_INVOKABLE void setProperty(QString name, QString section, QString key, QString value);
Q_INVOKABLE QString getProperty(QString name, QString section, QString key)
----------------qml文件中:
import MyConfiguration 1.0
// 实例化对象
MyConfiguration { id: myConfiguration }
myText.text = myConfiguration.getProperty("config.ini", "base", "name")
myConfiguration.setProperty("config.ini", "base", "age", "30")
方法二:将对象设置为上下文属性
-----------main.cc中:
QmlType qmlType; QQmlApplicationEngine engine; engine.rootContext()->setContextProperty("qmlTyppeTest", &qmlType);
-------qml文件中:
myText.text =qmlTyppeTest.getProperty("config.ini", "base", "name") qmlTyppeTest.setProperty("config.ini", "base", "age", "30")