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中运行,会报错.错误 ...
随机推荐
- 英语发音规则---C字母
英语发音规则---C字母 一.总结 一句话总结: 1.C发[k]音? cake [keɪk] n. 蛋糕 coat [kəʊt] n. 外套 music ['mjuːzɪk] n. 音乐,乐曲 pic ...
- Java IO 基础
早上复习了IO.NIO.AIO相关的概念,将其中一些要点记录一下. 从编程语言层面 BIO | NIO | AIO 以Java的角度,理解,linux c里也有AIO的概念(库),这些概念不知道什么原 ...
- windows下安装ImageMagick扩展
最近项目中需要用到图片的一些特殊处理——比如:根据用户请求生成任意尺寸的图像.经过一些资料的查找,最终选用了php_imagick.利用 ImageMagick,你可以根据web应用程序的需要动态生成 ...
- javascript中手风琴特效
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- node.js连接数据库登录注册,修改用户(页面的ajax请求)
首先给大家看一下目录结构,结构如下: index.html 首页(显示所有的用户信息) login.html 登录页 register.html 注册页 db.js 配置链接数据库参数 dbhelpe ...
- LeetCode(94)Binary Tree Inorder Traversal
题目如下: Python代码: def inorderTraversal(self, root): res = [] self.helper(root, res) return res def hel ...
- Arduino扫盲(持续添加中)
1.Arduino火的很,很大一点在于,他基本透明掉了硬件电子部分,只剩下软件部分,通过把电子部分包装成黑箱,使得大量IT人士,普通人,甚至小学生也能玩的来. 2 .Arduino是一个电子原型开发平 ...
- CF449D Jzzhu and Numbers (状压DP+容斥)
题目大意: 给出一个长度为n的序列,构造出一个序列使得它们的位与和为0,求方案数 也就是从序列里面选出一个非空子集使这些数按位与起来为0. 看了好久才明白题解在干嘛,我们先要表示出两两组合位与和为0的 ...
- 小学生都能学会的python(小数据池)
小学生都能学会的python(小数据池) 1. 小数据池. 目的:缓存我们字符串,整数,布尔值.在使用的时候不需要创建过多的对象 缓存:int, str, bool. int: 缓存范围 -5~256 ...
- 紫书 习题7-14 UVa 307(暴搜+剪枝)
这道题一开始我想的是在排序之后只在头和尾往中间靠近来找木块, 然后就WA, 事实证明这种方法是错误的. 然后参考了别人的博客.发现别人是直接暴搜, 但是加了很多剪枝, 所以不会超时. 我也想过这个做法 ...