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中运行,会报错.错误 ...
随机推荐
- EOJ 3000 ROT13加密和解密
应用 ROT13 到一段文字上仅仅只需要检查字母顺序并取代它在 13 位之后的对应字母,有需要超过时则重新绕回 26 英文字母开头即可.A 换成 N.B 换成 O.依此类推到 M 换成 Z,然后串行反 ...
- [转] Java 插入表记录后得到自增的id (附3种方法代码)
转自:https://blog.csdn.net/yaerfeng/article/details/7231093 在MySQL中,使用auto_increment类型的id字段作为表的主键,并用它作 ...
- .NET MVC Dropzone 上传图片
在nuget控制台输入:Install-Package dropzone @{ Layout = null; } <!DOCTYPE html> <html> <head ...
- Edge浏览器+微软小娜
- 开发辅助 | 前端开发工程师对 UI设计、交互设计的认知
1.UI 用户界面 UI:User Interfase 用户界面 UID:User Interfase Designer 用户界面设计师,多指移动 app 的界面设计: 2.一个合格的 UI 设计师, ...
- DataTables入门
转载 https://blog.csdn.net/gfd54gd5f46/article/details/65938189
- ZBrush常用快捷键
ZBrush是一款数字雕刻和绘画软件,它以强大的功能和直观的工作流程彻底改变了整个三维雕刻行业.强大的功能离不开便捷的操作,为此ZBrush®提供了一系列常用操作快捷键,熟练掌握这些快捷键,可帮助您节 ...
- J2EE概念汇总
JVM 是Java Virtual Machine(Java虚拟机)的缩写,JVM是一种用于计算设备的规范,它是一个虚构出来的计算机,是通过在实际的计算机上仿真模拟各种计算机功能来实现的.Java虚拟 ...
- BZOJ 1396 识别子串 (后缀自动机+线段树)
题目大意: 给你一个字符串S,求关于每个位置x的识别串T的最短长度,T必须满足覆盖x,且T在S中仅出现一次 神题 以节点x为结尾的识别串,必须满足它在$parent$树的子树中只有一个$endpos$ ...
- 笔记本安装Archlinux笔记
同步更新于wendster大佬的个人博客 搬运自我的洛谷博客 可能会不定期更新! 因为前几天给我的小炸鸡加了一根内存条:而且先前装的Xubuntu是32位的,使用极其不方便:再加上wendster大佬 ...