• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
Kevin Cheng's Yard
电脑是我的老婆,编程是我的灵魂,代码是我的语言,按键是我在歌唱。
https://github.com/surfsky/
博客园    首页    新随笔    联系   管理    订阅  订阅

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 }

 

转载请注明出处:http://surfsky.cnblogs.com 

posted @ 2015-03-27 15:59  surfsky  阅读(3703)  评论(1)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3