Qt qml 單例模式,沒什麼好說的,看代碼吧。單例模式很適合作全局的配置文件。ui
【示例下載】spa
http://download.csdn.net/detail/surfsky/8539313.net
【如下是核心代碼】code
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 }
qmldirblog
1 singleton Global Global.qml
TestSingleton.qmlclass
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 }