我没有使用QT创建者QT6为我的QML项目添加文件

发布于 2025-01-21 21:12:25 字数 980 浏览 0 评论 0原文

我感到有些ham愧,但我根本没有在QT Creator中的QML项目中添加文件。

我使用QT 6.2.3,我的构建系统是QMAKE。

首先,我通过右键单击一个仅包含main.qml文件的项目的文件夹来创建一个文件mybutton.qml。

然后,当我在main.qml中键入mybutton时,我可以使用自动完成,以便看到QT创建者检测到我的新类型。

但是在执行期间,我得到了一个错误:

QQmlApplicationEngine failed to load component
qrc:/MyProject/main.qml:9:5: MyButton is not a type

有人知道如何将文件添加到项目中吗?

我谨慎遵循正式文档( https://doc.qt.io of /QT-6/QTQML-DOCUMENTS-DEFINETYPES.HTML )和几个教程,但无效。太疯狂了。

编辑:

我做了Jarman建议的事情,但我仍然遇到同样的错误。

这是我的项目Arborescence 的图像

以及qml.qrc file的内容:

<RCC>
    <qresource prefix="/">
        <file>MyButton.qml</file>
        <file>main.qml</file>
    </qresource>
</RCC>

I feel a bit ashamed but I simply don't manage to add a file to my QML project in Qt Creator.

I use Qt 6.2.3 and my build system is qmake.

First off, I created a file MyButton.qml by doing a right click on the folder named QML of a project which only contains a main.qml file.

Then, when I type MyButton in main.qml I can use the autocompletion so I see Qt Creator detects my new type.

But during executation, I got this error :

QQmlApplicationEngine failed to load component
qrc:/MyProject/main.qml:9:5: MyButton is not a type

Anyone knows how to add a file to a project ?

I carefuly followed the official documentation (https://doc.qt.io/qt-6/qtqml-documents-definetypes.html) and several tutorials but nothing works. That's crazy.

EDIT :

I did what JarMan advises but I still get the same error.

Here is an image of my project arborescence

And the content of the qml.qrc file :

<RCC>
    <qresource prefix="/">
        <file>MyButton.qml</file>
        <file>main.qml</file>
    </qresource>
</RCC>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

暮年 2025-01-28 21:12:25

我相信您遇到的问题仅仅是您的可执行文件不知道在哪里可以找到.qml文件,因为构建文件夹通常与源文件夹不同。要解决此问题,您应该创建一个资源文件(您可以将其命名为像qml.qrc),并在其中列出所有QML文件。这允许QML文件捆绑到可执行文件中。

一个示例看起来像这样:

<RCC>
    <qresource prefix="/">
        <file>MyButton.qml</file>
    </qresource>
</RCC>

然后在.pro文件中,添加此行:

RESOURCES += qml.qrc

编辑:

您可以在QT Creator中做同样的事情。

步骤1:

您可能已经在项目中有一个资源文件。如果是这样,您将在窗口左侧的项目视图中看到它。如果这样做,则可以跳过步骤2。否则,转到file-&gt; new File ...,然后选择QT Resource File。将其命名qml.qrc(或任何您喜欢的)。

步骤2:

创建该文件后,现在应该显示在左侧的项目视图中。您可以右键单击文件名,然后选择添加现有文件。选择您的mybutton.qml文件。那应该完全按照我在原始答案中提到的确切操作,但不要手动键入任何代码。

I believe the problem you're running into is simply that your executable doesn't know where to find your .qml file because the build folder is usually not the same as the source folder. To fix this, you should create a resource file (you can name it something like qml.qrc) and list all of your QML files in there. This allows the QML file to get bundled into the executable.

An example would look like this:

<RCC>
    <qresource prefix="/">
        <file>MyButton.qml</file>
    </qresource>
</RCC>

Then in your .pro file, add this line:

RESOURCES += qml.qrc

EDIT:

You can do the same thing in Qt Creator.

Step 1:

You might already have a resource file in your project. If so, you'll see it in the Project view on the left side of the window. If you do, you can skip to Step 2. Otherwise, go to File->New File... and select Qt Resource File. Name it qml.qrc (or whatever you like).

Step 2:

Once you've created that file, it should now show up in the Project view on the left. You can right-click on the file name and select Add Existing Files. Choose your MyButton.qml file. That should do exactly what I mentioned in my original answer, but without manually typing any code.

月寒剑心 2025-01-28 21:12:25

当您选择最小必需的QT版本:对QT 6.2时,我发现QT 6.3.1的错误

也没有生成QML.QRC文件。

因此,我将最小必需的QT版本切换到QT创建者向导中的QT 5.15,QML.QRC文件将生成,我的自定义组件QML文件将自动包含在其中。

另外,.pro配置文件在这两个QT版本之间具有不同的项目。

I got the same error with Qt 6.3.1

I found the Qt Creator doesn't generate the qml.qrc file when you select Minimum required Qt version: to Qt 6.2.

So I switched the Minimum required Qt version: to Qt 5.15 in Qt Creator Wizard, the qml.qrc file will generated and my custom component qml file would be included automatically in it.

Also, the .pro configure file has different items between these two Qt version.

柏林苍穹下 2025-01-28 21:12:25

解决方案1:
在Pro文件中添加QML:
resources.files = main.qml mybutton.qml

解决方案2:
将QRC文件添加到项目中,然后添加QML作为资源文件

Solution 1:
add qml in the pro file:
resources.files = main.qml MyButton.qml

Solution 2:
add a qrc file into project and add qml as resource file

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文