zoukankan      html  css  js  c++  java
  • Qml 定义 constant

             对于程序中一些常量如字符串, 实数等, C++中经常用的方法, 是定义全局常量; 或者把所有意义相近的常量用一个单例类收集起来。

             QML是类JSON的标识性语言, 使用js 语法去操作对象。 在QML中定义常量稍稍麻烦些。 总结起来, 有以下几种方法:

             使用 C++   qml 混合编程时:

           可以在 C++ 中定义可读属性,或者使用 Q_INVOKABLE函数只提供get方法;  再把这个C++类暴露给QML——该方法效果同于在 QML 中使用QtObject定义不可见类

          另一种方法是使用QQmlEngine 的register none instantable 函数簇定义不能被实例化的类型。  

          qmlRegisterInterface
          qmlRegisterSingletonType

         然后在QML 使用 import 引入自定义命名空间以后,使用 Type.member 引用相关属性, 或者方法。  

     另一种是 在 Python 文件中定义一个dict-lists 集合, 然后在QML中引用。 具体用法参考

          --Defining-and-using-constants-from-PySide-in-QML

             上述两种方法都需要从 QqmlEngine 获取 rootContext, 并使用setContextProperty注册。

       还有一种方法是 在 js 文件中定义变量, 然后在 qml中使用。

       对于 list 类型, C++ 的处理方法是 使用 QQmlListProperty 封装Qlist类, 也可以封装自定义容器; 在js 和 python中, 直接给list 写入字面量即可。注意, 在使用 QQmlListProperty 时, 如果要定义 list<Fruit>, 需要定义一个 QList<Fruit*>作为 QQmlListProperty<int> 的参数传入, 但是 fruit必须是 QObject的子类。

  • 相关阅读:
    LeetCode 252. Meeting Rooms
    LeetCode 161. One Edit Distance
    LeetCode 156. Binary Tree Upside Down
    LeetCode 173. Binary Search Tree Iterator
    LeetCode 285. Inorder Successor in BST
    LeetCode 305. Number of Islands II
    LeetCode 272. Closest Binary Search Tree Value II
    LeetCode 270. Closest Binary Search Tree Value
    LeetCode 329. Longest Increasing Path in a Matrix
    LintCode Subtree
  • 原文地址:https://www.cnblogs.com/aslistener/p/4507751.html
Copyright © 2011-2022 走看看