我正在尝试使用 PyQt5 和 python 构建 UI。我使用 main.qml 和同一目录中的一些样式化的 qml 文件构建了一个基础,并且导入得很好,但我决定将所有内容分解到文件夹中,以便它更有组织性,但现在无论我做什么,我的导入都不起作用。
新的文件结构是:
Interface/
│ ├── main.py
│ ├── resources.qrc
│ ├── resources_rc.py
│ │
│ ├── src/
│ │ ├── main.qml
│ │ │
│ │ ├── Components/ # Directory for custom QML components
│ │ │ ├── Sections/ # Directory for "Sections" module
│ │ │ │ ├── Status.qml
├── Title.qml
│ │ │ │ └── qmldir # qmldir file for "Sections" module
│ │ │ │
│ │ │ └── StyledElements/ # Directory for "StyledElements" module
│ │ │ ├── StyledButton.qml
│ │ │ └── qmldir
StyledElements & Sections 包含一个 qmldir 文件,如下所示:
module Sections
Status 1.0 Status.qml
Title 1.0 Title.qml
我的main.py这样称呼它:
import os
import sys
from PyQt5.QtWidgets import QApplication
from PyQt5.QtQml import QQmlApplicationEngine
def handle_warnings(warnings):
for warning in warnings:
print(f"QML Warning: {warning.toString()}")
app = QApplication([])
engine = QQmlApplicationEngine()
engine.warnings.connect(handle_warnings)
# Load QML file
qml_file_path = os.path.abspath('src/main.qml')
print(f"Loading QML file: {qml_file_path}")
engine.load(qml_file_path)
if not engine.rootObjects():
print("Failed to load QML file.")
sys.exit(-1)
else:
print("QML file loaded successfully.")
app.exec_()
sys.exit(app.exec_())
我的 main.qml 看起来像:
import QtQuick 2.15
import QtQuick.Controls 2.15
import StyledElements 1.0
import Sections 1.0
ApplicationWindow {
title: 'Interceptor'
visible: true
width: 700
height: 600
Rectangle {
id: rectangle
width: 700
height: 725
color: 'white'
Section_Title{}
StyledButton{}
}
}
总是让我遇到这些错误:
QML Warning: module "Sections" is not installed
QML Warning: module "StyledElements" is not installed
QML Warning: module "Sections" is not installed
QML Warning: module "StyledElements" is not installed
我尝试过的事情:
我尝试在 main.qml 中的导入周围加上引号,例如:
import "StyledElements" 1.0
import "Sections" 1.0
我尝试过像这样设置导入路径:
# Set the path to the QML files
engine.addImportPath(os.path.abspath("src/Components/Sections"))
engine.addImportPath(os.path.abspath("src/Components/StyledElements"))
# Load QML file
qml_file_path = os.path.abspath('src/main.qml')
engine.load(qml_file_path)
还尝试过:
engine.addImportPath(os.path.abspath("qrc:/"))
or
engine.addImportPath(os.path.abspath("qrc:/src/main.qml"))
据我所知,问题与加载 main.qml 无关,因为当我更改路径时,它会给我一个关于未找到该路径的错误,并且该错误似乎来自 main.qml 中的导入。
我尝试使用如下所示的资源文件:
<RCC>
<qresource prefix="/">
<file>src/main.qml</file>
<file>src/Components/Sections/NvStatus.qml</file>
<file>src/Components/Sections/qmldir</file>
<file>src/Components/StyledElements/StyledButton.qml</file>
<file>src/Components/StyledElements/qmldir</file>
</qresource>
</RCC>
我将资源文件编译成Python
pyrcc5 resources.qrc -o resources_rc.py
& 在 main.py 中使用导入了新的 py 文件
import resources_rc
此时,我花了几个小时使用 Chat GPT 和谷歌试图找到答案。我已经尝试了多种“有效”的方法,即它们最终加载了 qml,但无法加载自定义模块。我不知道如何继续。我尝试的所有操作都导致我遇到了 main.qml 无法加载模块并出现上面显示的错误的相同问题
在用头撞墙几个小时后,我想通了:
最大的问题是我如何将模块导入到 main.qml 中。它希望您在导入中添加文件夹/子文件夹,如下所示:
以前:
import StyledElements 1.0
import Sections 1.0
已修复:
import src.Components.StyledElements 1.0
import src.Components.Sections 1.0
之后就像使用以下命令重新运行我的 qrc 转换为 python 脚本一样简单:
pyrcc5 resources.qrc -o resources_rc.py
在这里找到信息:https://doc.qt.io/qt-6/qtqml-modules-identifiedmodules.html