在 Qt Creator 中相互暴露 QML 组件 [英] Exposing QML Components to each other in Qt Creator

查看:72
本文介绍了在 Qt Creator 中相互暴露 QML 组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Qt-Quick 和 PySide2 开发仪表板应用程序,但在 Qt Creator 的设计模式下无法公开我的 QML 组件.

我的文件夹结构如下所示:

myapp/├──我的组件│ ├── component1.qml│ └── component2.qml│ └── 等等.├── 页面│ ├── page1.qml│ └── page2.qml└── app.qml

我可以使用 myapp/mycomponents 中的所有组件,方法是使用相对路径导入适当的 .qml 文件或其父目录:

//在 page1.qml 里面导入../mycomponents"物品 {编号:第 1 页我的组件 1 {//一些代码}}

问题是当我打开 page.qml 时,我无法在 My QML Components 标签中看到来自 myapp/components 的组件(或同级目录中的任何其他 .qml 文件)在 Qt Creator --> Design 中的文件.

app.qml 可用的所有组件

但不在 page1.qml 中

如何在不改变文件夹结构的情况下将 myapp/components 暴露给位于同级目录中的 .qml 文件我的项目?

编辑 1:我已经阅读了关于

不幸的是,您必须关闭并重新打开项目才能看到结果.

我准备了一个小例子这里

I'm developing a dashboard application using Qt-Quick with PySide2 and am having trouble exposing my QML components in Qt Creator's design mode.

My folder structure looks something like this:

myapp/
├── mycomponents
│   ├── component1.qml
│   └── component2.qml
│   └── etc..
├── pages
│   ├── page1.qml
│   └── page2.qml
└── app.qml

I can use all components from myapp/mycomponents by importing the appropriate .qml files or it's parent directory using relative paths:

// inside page1.qml

import "../mycomponents"

Item {
    id:page1

    MyComponent1 {
        // some code
    }

}

The problem is that I'm unable to see the components from myapp/components in the My QML Components tab when I open up a page.qml (or any other .qml file in a sibling directory) file in Qt Creator --> Design.

All components available to app.qml

But not in page1.qml

How can I expose myapp/components to .qml files located in sibling directories without changing the folder structure of my project?

Edit 1: I've already read docs regarding designer.metainfo but they all seem to refer to plugin applications which mine isn't. So I have a hard time making these work for my use case.

解决方案

The designer only shows components from the current directories or from used imports(which can be a c++ plugin, but also qmldir with some qml components instead). So you have to create an import and add that somehow to the loaded project - not sure what pyside uses, but maybe it is a good idea to create a myapp.qmlproject file for all the qml stuff and inside that you need:

importPaths: [ "." ]

then inside the mycomponents directory, you need a qmldir file

Component1 1.0 Component1.qml

also, you need the designer directory with the file mycomponents.metainfo

MetaInfo {
    Type {
        name: "Component1"

        ItemLibraryEntry {
            name: "a Test Component1"
            category: "mycomponents"
            version: "1.0"
            requiredImport: "mycomponents"
        }
    }
}

Unfortunately, you have to close and reopen the project to see the result.

I prepared a small example here

这篇关于在 Qt Creator 中相互暴露 QML 组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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