Qt qml 单例模式

时间:2023-03-09 06:25:59
Qt qml 单例模式

Qt qml 单例模式,没什么好说的,看代码吧。单例模式很适合做全局的配置文件。

【示例下载】

http://download.csdn.net/detail/surfsky/8539313

【以下是核心代码】

Global.qml

 pragma Singleton
import QtQuick 2.0
QtObject {
property color bgColor: 'lightblue';
property int textSize:
property color textColor: "green"
}

qmldir

 singleton Global Global.qml

TestSingleton.qml

 import QtQuick 2.0
import "." // 单例模式必须显式的调用qmldir文件
Rectangle {
width:
height:
color: Global.bgColor;
Text{
text: 'hello world'
font.pixelSize: Global.textSize // 调用单例对象的属性
color: Global.textColor
anchors.centerIn: parent
}
}