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中运行,会报错.错误 ...
随机推荐
- c13--数组
// // main.c // 进制查表法 // // Created by xiaomage on 15/6/10. // Copyright (c) 2015年 xiaomage. All rig ...
- webview同步cookies
目前很多android app都内置了可以显示web页面的界面,会发现这个界面一般都是由一个叫做WebView的组件渲染出来的,学习该组件可以为你的app开发提升扩展性. 先说下WebView的一些优 ...
- c语言循环案例
do while #include <stdio.h> #include <stdlib.h> int main() { int a = 1,b = 10; do { b -= ...
- Docker容器查看ip地址
第一步:进入centos7容器:yum install net-tools -y 我这里已经加载过,所以没有继续加载 第二步:加载完成之后可以输入 ifconfig查看ip地址
- 19. Remove Nth Node From End of List[M]删除链表的倒数第N个节点
题目 Given a linked list, remove the n-th node from the end of list and return its head. *Example: Giv ...
- C# 异步编程学习(一)
异步 编程 可在 等待 某个 任务 完成时, 避免 线程 的 占用, 但要 想 正确地 实现 编程, 仍然 十分 伤脑筋. . NET Framework 中, 有三种 不同 的 模型 来 简化 异步 ...
- 安装wampserver遇到的问题及解决方案
丢失api-ms-win-crt-runtime-l1-1-0.dll 安装完wampserver,启动服务器的时候遇到一些问题,提示说缺失dll文件,如下图所示: 网上一搜,很多人出现过丢失api- ...
- Ubuntu_18.04安装网易云音乐
首先到网易云音乐官网下载网易云音乐,ubuntu版的,安装. 这时候的图标打不开,缺少libcanberra sudo apt install libcanberra-gtk-module 安装完了配 ...
- 【Latex常见问题总结】
1. 非数学符号如max/min将下标放到正下方,这个问题折腾了很久, 下标不在正下方会带俩两个问题,一是有时候不够美观,二是会使得数学公式过长越界,需要换行. 解决方案:将符号转换为数学符号, \m ...
- s5pv210 fimc 之 fimc-dev.c
fimc-dev.c 是Samsung FIMC 设备的V4L2 驱动.上层应用直接操作这个设备,进行capture,图片处理,以及overlay输出 http://blog.csdn.net/cxw ...