使用qmldir在qml中添加新的module

  //Style.qml with custom singleton type definition
  pragma Singleton
  import QtQuick 2.0

  QtObject {
      property int textSize: 20
      property color textColor: "green"
  }

  // qmldir declaring the singleton type
  module CustomStyles
  singleton Style 1.0 Style.qml

  // singleton type in use
  import QtQuick 2.0
  import CustomStyles 1.0

  Text {
      font.pixelSize: Style.textSize
      color: Style.textColor
      text: "Hello World"
  }
 

这是Qt中示范的写法。

在qmldir中,module名字是当前要引入qml文件的具体目录。

文件结构:

使用qmldir在qml中添加新的module_第1张图片

qmldir写法:

使用qmldir在qml中添加新的module_第2张图片

之后再在main.cpp中添加import路径:

engine.addImportPath(QStringLiteral("qrc:/"));

使用的时候直接

import qml.Controls 1.0

你可能感兴趣的:(QML,qt)