Qt qml 单例模式

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 }
相关文章
相关标签/搜索