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中运行,会报错.错误 ...
随机推荐
- 利用CSS3中的clac()实现按照屏幕分辨率自适应宽度
1.简介 calc()看其外表像个函数.平时在制作页面的时候,总会碰到有的元素是100%的宽度(例如body元素).如果元素宽度为100%时,其自身不带其他盒模型属性设置还好,要是有别的,那将导致盒子 ...
- [转]Linux+XAMPP+eolinker开源版v3.2.4
eolinker是一个由国人开源的接口管理系统(AMS),特性及介绍详见开源中国-eolinker首页. 搭建步骤参考:eolinker开源指南 系统环境:CentOS Linux release 7 ...
- Caffe solver.prototxt学习
在solver解决下面的四个问题: a.训练的记录(bookkeeping),创建用于training以及test的网络结构: b.使用前向以及反向过程对training网络参数学习的过程: c.对t ...
- SQL学习——小结练习(1)
到处淘来的SQL题 1. 用一条SQL 语句 查询出每门课都大于80 分的学生姓名 name kecheng fenshu 张三 语文 81张三 数学 ...
- Broadcast Receiver广播接收器
1.概述 广播接收器不仅能接受来自系统的内容,也可以接受来自其他app的内容.广播分为标准广播和有序广播. 2.标准广播 一种完全异步执行的广播,在广播发出之后几乎所有的广播接收器都在同一时刻接受到广 ...
- maridb Error 'Operation DROP USER failed for
数据库版本:mariadb 10.0.12 主库删除多余的用户名,因从库没有此信息造成主从故障! 报错信息如下:Error 'Operation DROP USER failed for 'use ...
- Python2以及Python3中的除法
前言 在讨论话题之前,我们先说下程序中除法的三种情况: 1. 传统的除法,我称之为整型地板除.在C.C++.Java中常见,特点是整数相除舍弃小数取整,浮点数相除则保留小数(如果有). >> ...
- PhotoZoom的工具栏 图片放大不失真
使用PhotoZoom能够对数码图片无损放大,备受设计师和业内人员的青睐,它的出现时一场技术的革新,新颖的技术,简单的界面,优化的算法,使得它可以对图片进行放大而没有锯齿,不会失真.本文为您一起来认识 ...
- 蓝桥杯_left and throw
思考了许久没有结果,最后,还是一位擅长搜索资源的学长帮我找到了一个不错的代码,这个代码极其精妙,再一次印证了一句话,没有做不到的,只有想不到的,当然这个代码我拿到手的时候是个没有注释的代码,我费尽周折 ...
- java开发移动端之spring的restful风格定义
https://www.ibm.com/developerworks/cn/web/wa-spring3webserv/index.html