我试图在QML中添加FileDialog
,我的环境:
- PyQt6
- Arch Linux
qt6-base
,qt6-declarative
包安装 代码:
main.qml
import QtQuick
import QtQuick.Window
import QtQuick.Controls
import QtQuick.Dialogs
Window {
Rectangle {
id: mainRect
anchors.fill: parent
Button {
text: qsTr("Open File")
onClicked: fileDialog.open()
}
}
FileDialog {
id: fileDialog
}
}
main.py
import os
import sys
from PyQt6.QtGui import QGuiApplication
from PyQt6.QtQml import QQmlApplicationEngine
def main():
app = QGuiApplication(sys.argv)
engine = QQmlApplicationEngine()
engine.load(os.fspath(Path(__file__).resolve().parent / "qml/main.qml"))
if not engine.rootObjects():
sys.exit(-1)
sys.exit(app.exec())
if __name__ == "__main__":
main()
当我尝试运行时,它会产生以下错误:
file:///main.qml:20:5: QML FileDialog: Failed to load non-native FileDialog implementation:
qrc:/qt-project.org/imports/QtQuick/Dialogs/quickimpl/qml/FileDialog.qml:4 module "Qt.labs.folderlistmodel" is
not installed
Segmentation fault (core dumped)
我试着搜索包的repo和pypi,看看是否有一个额外的包包含这个文件夹列表模型,但我找不到任何。
我在Windows上尝试过FileDialog
,它工作得很好,这似乎是一个Arch Linux包装问题,但我不确定在哪里报告。我使用tkinter
在这个答案中提到的解决方案,它在两个操作系统上都能很好地工作。
必须安装pyside6:
export QML2_IMPORT_PATH=/usr/local/lib/python3.8/site-packages/PySide6/Qt/qml
在码头工人:
ENV QML2_IMPORT_PATH=/usr/local/lib/python3.8/site-packages/PySide6/Qt/qml