Defining and using constants from PySide in QML
Defining and using constants from PySide in QML
This PySide tutorial shows you how to define constant values (with hierarchical structures) and expose them to QML. This allows you to define often-used constants (e.g. the size of a finger-friendly widget) and change them globally in your UI simply by changing the code in your Python script. Of course, as it is a simple Python Dictionary, you can also generate the data programatically. Be aware that this example does not allow you to change the values once you have set the context property (for this, you would need a QObject with a NOTIFYable properties).
Constants.py
Importing the required modules
We need the sys module for command line parameters and to give the correct exit status. We also need QtCore, QtGui and QtDeclarative to set up our UI:
Defining the constants as Python dict
Simply create a dictionary – it should contain basic data types (e.g. str, float, int, bool), dictionaries (dict) or lists (list). You can nest lists and dicts:
- Constants = {
- 'CustomText': "Hey PySide!",
- 'FontSize': 9.24,
- 'Colors': {
- 'Background': "#8ac852",
- 'Foreground': "#00672a",
- },
- 'BoldText': True,
- 'Items': {
- 'Count': 7,
- 'Suffixes': ['A', 'B', 'C', 'D', 'E', 'F', 'G'],
- },
- 'Step': { 'X': 5, 'Y': 10 },
- }
Creating QApplication and QDeclarativeView
This is easy – simply create a new QApplication, passing the command line parameters to its constructor. Then create a QDeclarativeView and configure it so that whenever the window is resized, the root object automatically changes size as well.
- app = QtGui.QApplication(sys.argv)
- view = QtDeclarative.QDeclarativeView()
- view.setResizeMode(QtDeclarative.QDeclarativeView.SizeRootObjectToView)
Inject the constants as context property
Get the root context of the QML engine via rootContext(), then use setContextProperty to expose the constants dict to the QML world:
- ctx = view.rootContext()
- ctx.setContextProperty('C', Constants)
Load QML, show window and run application
Assuming the QML file lies in the current directory, simply set its filename with setSource on the view. Then, let the window appear with show() and finally start the application using exec_() on our QApplication instance.
- view.setSource('Constants.qml')
- view.show()
- sys.exit(app.exec_())
Constants.qml
Now that you have “injected” your constants as “C” context property, you can now access its items as if they were attributes. This also works for nested dictionaries (e.g. C.Items.Count) and also for lists (e.g. C.Items.Suffixes[index]). Be aware that with this approach, you cannot change constants later (e.g. when you want to change the background color at runtime or something.
- import Qt 4.7
- Rectangle {
- width: 400
- height: 400
- color: C.Colors.Background
- Repeater {
- model: C.Items.Count
- Text {
- y: index * C.Step.Y
- x: index * C.Step.X
- color: C.Colors.Foreground
- font.bold: C.BoldText
- font.pointSize: C.FontSize
- text: C.CustomText + C.Items.Suffixes[index]
- }
- }
- }
Defining and using constants from PySide in QML的更多相关文章
- 转载 C#中敏捷开发规范
转载原地址 http://www.cnblogs.com/weixing/archive/2012/03/05/2380492.html 1.命名规则和风格 Naming Conventions an ...
- linux c coding style
Linux kernel coding style This is a short document describing the preferred coding style for the lin ...
- [中英对照]Linux kernel coding style | Linux内核编码风格
Linux kernel coding style | Linux内核编码风格 This is a short document describing the preferred coding sty ...
- SubmittingPatches, SubmitChecklist and CodingStyle
How to Get Your Change Into the Linux Kernel or Care And Operation Of Your Linus Torvalds For a pers ...
- CLR via C# 3rd - 07 - Constants and Fields
1. Constants A constant is a symbol that has a never-changing value. When defining a constant ...
- 7.Constants and Fields
1.Constants is a symbol that has a never-changing value. its value must be determinable at compile ...
- 初识QML学习机制
在QML中,一个用户界面被指定为具有属性的对象树,这使得Qt更加便于很少或没有编程经验的人使用,JavaScript在QML中作为一种脚本语言,对QML进行逻辑方面的编程. AD:WOT2015 互联 ...
- QML Object Attributes QML对象属性
QML Object Attributes Every QML object type has a defined set of attributes. Each instance of an obj ...
- 如何实现PyQt5与QML响应彼此发送的信号?
对于PyQt5+QML+Python3混合编程,如何实现PyQt5与QML响应彼此发送的信号,这是一个棘手的问题. 大抵有如下五种方式: (要运行下面五个例子,千万不能在eric6中运行,会报错.错误 ...
随机推荐
- POJ 2446 匈牙利算法
题意: 思路: 二分图匹配... // by SiriusRen #include <cmath> #include <cstdio> #include <cstring ...
- spring《四》自动装配
byName模式<bean autowire="byName"> Spring会查找一个叫做date的bean定义. byType模式<bean autowire ...
- 洛谷P3356 火星探险问题(费用流)
题目描述 火星探险队的登陆舱将在火星表面着陆,登陆舱内有多部障碍物探测车.登陆舱着陆后,探测车将离开登陆舱向先期到达的传送器方向移动.探测车在移动中还必须采集岩石标本.每一块岩石标本由最先遇到它的探测 ...
- Java NIO(三)通道
概念 通道(Channel)由java.nio.channels包定义的.channel表示IO源与目标打开的连接,类似流,但不能直接访问数据,只能与Buffer进行交互 通道类似流,但又有不同: 既 ...
- Java操作Kafka执行不成功
使用kafka-clients操作kafka始终不成功,原因不清楚,下面贴出相关代码及配置,请懂得指点一下,谢谢! 环境及依赖 <dependency> <groupId>or ...
- 在应用层通过spring特性解决数据库读写分离
如何配置mysql数据库的主从? 单机配置mysql主从:http://my.oschina.net/god/blog/496 常见的解决数据库读写分离有两种方案 1.应用层 http://neore ...
- asp实现阿里大鱼短信API接口的方法
阿里大鱼是阿里推出的产品,官方提供JAVA..NET.PHP等版本的SDK下载,不知为何,唯独不提供ASP版本的SDK. 不提供没关系,自己写就是了,参照官方提供的API写一个就是了. 本来以为无非是 ...
- UNP学习笔记4——I/O复用:select和poll函数
1 概述 之间的学习中发现,传统的阻塞式系统调用不仅浪费进程运行时间,而且会带来狠毒问题.因此进程需要有一种预先告知内核的能力,使得内核一旦发现进程指定的一个或者多个I/O条件就绪,它就通知进程.这个 ...
- mac 调整Launchpad行列数目
Mac调整Launchpad 行数和列数 开打Terminal终端依次输入以下三句: <1> 改变行数:defaults write com.apple.dock springboard ...
- Kattis - Babelfish
Babelfish You have just moved from Waterloo to a big city. The people here speak an incomprehensible ...