Qt qml 單例模式,沒什么好說的,看代碼吧。單例模式很適合做全局的配置文件。
【示例下載】
http://download.csdn.net/detail/surfsky/8539313
【以下是核心代碼】
Global.qml
1 pragma Singleton 2 import QtQuick 2.0 3 QtObject { 4 property color bgColor: 'lightblue'; 5 property int textSize: 20 6 property color textColor: "green" 7 }
qmldir
1 singleton Global Global.qml
TestSingleton.qml
1 import QtQuick 2.0 2 import "." // 單例模式必須顯式的調用qmldir文件 3 Rectangle { 4 width: 500 5 height: 500 6 color: Global.bgColor; 7 Text{ 8 text: 'hello world' 9 font.pixelSize: Global.textSize // 調用單例對象的屬性 10 color: Global.textColor 11 anchors.centerIn: parent 12 } 13 }