Новая структура файла:
Код: Выделить всё
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
Код: Выделить всё
module Sections
Status 1.0 Status.qml
Title 1.0 Title.qml
Код: Выделить всё
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_())
Код: Выделить всё
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"))
Я попробовал использовать файл ресурсов, который выглядит так:
Код: Выделить всё
src/main.qml
src/Components/Sections/NvStatus.qml
src/Components/Sections/qmldir
src/Components/StyledElements/StyledButton.qml
src/Components/StyledElements/qmldir
Код: Выделить всё
pyrcc5 resources.qrc -o resources_rc.pyКод: Выделить всё
import resources_rcПодробнее здесь: https://stackoverflow.com/questions/786 ... sing-pyqt5