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 }

 

相关文章:

  • 2021-09-21
  • 2022-12-23
  • 2022-12-23
  • 2021-06-16
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-26
猜你喜欢
  • 2021-07-29
  • 2022-02-16
  • 2022-12-23
  • 2022-12-23
  • 2021-12-03
  • 2021-09-06
相关资源
相似解决方案