我创建了一个库来存储我正在为应用程序制作的自定义QML组件。我正在与python一起使用Pyside6。这是基本目录的样子:
library
| |__CustomComponents
| |__Components1
| |__CustomComponent1.qml
|
|__test
|__MainComponent
|__main.py
|__Main.qml库和测试在同一个层次上。我希望能够访问CustomComponent1.qml文件,以便在Main.qml中使用,但是如何用Python编写导入语句呢?另外,为了能够将Components1作为一个模块导入,我所需要的qmldir文件就足够了吗?如果有一个命令可以用来生成生成模块所需的文件,那就太好了。
编辑:可以同时使用QQuickView和QQmlApplicationEngine吗?
view = QQuickView()
view.setResizeMode(QQuickView.SizeRootObjectToView);
newWidget = newWidget()
view.rootContext().setContextProperty("headerWidget", headerWidget)
qml_file = os.fspath(Path(__file__).resolve().parent / 'Main.qml')
view.setSource(QUrl.fromLocalFile(qml_file))
if view.status() == QQuickView.Error:
sys.exit(-1)
view.show()发布于 2021-06-09 01:33:19
您必须使用qmldir来指示该文件夹中有一个模块,要使用它,必须在引擎导入列表中使用它,为此您必须使用addImportPath方法。
├── library
│ └── CustomComponents
│ └── Components1
│ ├── CustomComponent1.qml
│ └── qmldir
└── test
└── MainComponent
├── main.py
└── main.qmlCustomComponent1.qml
import QtQuick
Rectangle{
id: root
width: 100
height: 100
color: "salmon"
}qmldir
module Components1
CustomComponent1 CustomComponent1.qmlmain.py
import os
from pathlib import Path
import sys
from PySide6.QtCore import QCoreApplication, Qt, QUrl
from PySide6.QtGui import QGuiApplication
from PySide6.QtQml import QQmlApplicationEngine
CURRENT_DIRECTORY = Path(__file__).resolve().parent
LIBRARY_DIR = CURRENT_DIRECTORY.parents[1] / "library" / "CustomComponents"
def main():
app = QGuiApplication(sys.argv)
engine = QQmlApplicationEngine()
engine.addImportPath(os.fspath(LIBRARY_DIR))
url = QUrl.fromLocalFile(os.fspath(CURRENT_DIRECTORY / "main.qml"))
def handle_object_created(obj, obj_url):
if obj is None and url == obj_url:
QCoreApplication.exit(-1)
engine.objectCreated.connect(
handle_object_created, Qt.ConnectionType.QueuedConnection
)
engine.load(url)
sys.exit(app.exec())
if __name__ == "__main__":
main()main.qml
import QtQuick
import QtQuick.Window
import Components1
Window {
width: 640
height: 480
visible: true
title: qsTr("Hello World")
CustomComponent1{
}
}QQuickView
对于QQuickView,它有一个引擎,因此您必须使用该引擎,因此所做的更改如下:
main.py
import os
from pathlib import Path
import sys
from PySide6.QtCore import QCoreApplication, Qt, QUrl
from PySide6.QtGui import QGuiApplication
from PySide6.QtQuick import QQuickView
CURRENT_DIRECTORY = Path(__file__).resolve().parent
LIBRARY_DIR = CURRENT_DIRECTORY.parents[1] / "library" / "CustomComponents"
def main():
app = QGuiApplication(sys.argv)
view = QQuickView()
view.engine().addImportPath(os.fspath(LIBRARY_DIR))
url = QUrl.fromLocalFile(os.fspath(CURRENT_DIRECTORY / "main.qml"))
def handle_status_changed(status):
if status == QQuickView.Error:
QCoreApplication.exit(-1)
view.statusChanged.connect(
handle_status_changed, Qt.ConnectionType.QueuedConnection
)
view.setSource(url)
view.show()
sys.exit(app.exec())
if __name__ == "__main__":
main()main.qml
import QtQuick
import Components1
Rectangle {
width: 640
height: 480
CustomComponent1{
}
}https://stackoverflow.com/questions/67890679
复制相似问题