Qt qml 單例模式


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 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM