包含QML文件中的另一个QML文件 [英] Include another QML file from a QML file

查看:1103
本文介绍了包含QML文件中的另一个QML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

还有关于此问题的Stackoverflow的另一个问题,但我没有找到可接受的解决方案。所以我再问一遍,因为旧的问题没有引起注意。

There's another question on Stackoverflow about this matter but I don't find the accepted solution possible. So I ask again because the old question is out of attention.

情况就是这样。我有'main.qml','feature1.qml','feature2.qml'定义的应用程序屏幕。

The situation is this way. I have application screens defined by 'main.qml', 'feature1.qml', 'feature2.qml'.

这些屏幕共享标题栏下方的相同工具栏。工具栏有多个项目,因此复制粘贴QML代码就像疯了一样。这个问题: QML文件包含 - 或一个单片文件(结构QML代码)?说可以只使用QML文件名作为组件名称,但我无法使其正常工作。

These screens share the same toolbar below title bar. The toolbar has multiple items so copy-paste the QML code is like crazy. This question: QML file include - or one monolithic file (structure QML code)? says it's possible to just use QML file name as component name but I can't get it working.

任何解决方案?详情请参阅。

Any solution? with details pls.

推荐答案

我们假设您有一个名为 main.qml的文件和另一个名为 MyCustomText.qml 的文件中的组件。如果两个文件都在同一目录中,您可以直接加载组件,如下所示:

Let's assume you have a file called main.qml and a component in another file called MyCustomText.qml. If both files are in the same directory you can directly load the component like this:

// in Main.qml
Rectangle {
  id: root
  MyCustomText {
    text: "This is my custom text element"
  }
}

如果 MyCustomText.qml 在另一个子目录 MyComponents 例如,要将所有自定义组件组合在一起,首先需要 import 该目录,然后才能以相同的方式使用该组件:

If MyCustomText.qml is in another subdirectory MyComponents for example to group all your custom components together, you first need to import the directory before using the component the same way:

// in Main.qml
import "MyComponents"

Rectangle {
  id: root
  MyCustomText {
    text: "This is my custom text element"
  }
}

另一个需要注意的重要事项是, QML 文件应始终以大写字母开头,如果您希望能够以这种方式使用它们

Another important thing to note is that your QML files should always start with an uppercase letter if you want to be able to use them this way

当然你的 Loader 解决方案也有效,但是s是在其他组件中导入QML文件的最简单方法。

Of course your Loader solution works too but this is the easiest way to import QML files in other components.

这篇关于包含QML文件中的另一个QML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆