zoukankan      html  css  js  c++  java
  • 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:

    1. import sys
    2.  
    3. from PySide import QtCore
    4. from PySide import QtGui
    5. from PySide import QtDeclarative

    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:

    1. Constants = {
    2.     'CustomText': "Hey PySide!",
    3.     'FontSize': 9.24,
    4.     'Colors': {
    5.         'Background': "#8ac852",
    6.         'Foreground': "#00672a",
    7.     },
    8.     'BoldText': True,
    9.     'Items': {
    10.         'Count': 7,
    11.         'Suffixes': ['A', 'B', 'C', 'D', 'E', 'F', 'G'],
    12.     },
    13.     'Step': { 'X': 5, 'Y': 10 },
    14. }

    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.

    1. app = QtGui.QApplication(sys.argv)
    2.  
    3. view = QtDeclarative.QDeclarativeView()
    4. 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:

    1. ctx = view.rootContext()
    2. 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.

    1. view.setSource('Constants.qml')
    2. view.show()
    3.  
    4. 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.

    1. import Qt 4.7
    2.  
    3. Rectangle {
    4.     width: 400
    5.     height: 400
    6.     color: C.Colors.Background
    7.  
    8.     Repeater {
    9.         model: C.Items.Count
    10.  
    11.         Text {
    12.             y: index * C.Step.Y
    13.             x: index * C.Step.X
    14.             color: C.Colors.Foreground
    15.             font.bold: C.BoldText
    16.             font.pointSize: C.FontSize
    17.             text: C.CustomText + C.Items.Suffixes[index]
    18.         }
    19.     }
    20. }
  • 相关阅读:
    奇 arch/i386/kernel/head.o(.text+0x3e): undefined reference to `stack_start'
    惊爆:当Python代码遇到zip解压炸弹,未做防护的你后悔莫及!
    肝了1个月,做了10个Python可视化动图,有需要的自己拿
    抖音超火的九宫格视频是如何生成的,Python 告诉你答案
    找出文件夹中的相同文件,并移动到指定文件夹中
    8行Python代码绘制疫情地图
    怎么将python项目打包成exe程序?
    利用Pycharm + Django搭建一个简单Python Web项目
    转行Python会经历的几个学习阶段!未来有哪些就业方向?
    一个python脚本就可以B站查找弹幕发送者!
  • 原文地址:https://www.cnblogs.com/luomingchuan/p/3585103.html
Copyright © 2011-2022 走看看