问题描述
在我的 Windows 10 机器上,我正在尝试一些简单的新 Qt6 示例,而基于 QML 的示例对我不起作用。
我正在运行 Python 3.8.6 和一个虚拟环境
python3 -m venv venv
.\venv\Scripts\Activate.ps1
pyside6 安装到 venv 时没有任何警告
pip install pyside6
使用 QApplication 和 QLabel 的非 QML hello world 示例运行良好(这个:https://doc.qt.io/qtforpython/tutorials/basictutorial/widgets.html)
这个例子不起作用,取自 https://doc.qt.io/qtforpython/tutorials/basictutorial/qml.html :
main.py:
from PySide6.QtWidgets import QApplication
from PySide6.QtQuick import qquickview
from PySide6.QtCore import QUrl
app = QApplication([])
view = qquickview()
url = QUrl("view.qml")
view.setSource(url)
view.show()
app.exec_()
view.qml:
import QtQuick 2.0
Rectangle {
width: 200
height: 200
color: "green"
Text {
text: "Hello World"
anchors.centerIn: parent
}
}
我收到的尝试运行的消息是:
file:///C:/github/aorcl/python-gui-2/view.qml:1:1: Cannot load library C:\github\aorcl\python-gui-2\venv\lib\site-packages\PySide6\qml\QtQml\WorkerScript\workerscriptplugin.dll: The specified module Could not be found.
import QtQuick
^
file:///C:/github/aorcl/python-gui-2/view.qml: Failed to load dependencies for module "QtQml" version 6.0
file:///C:/github/aorcl/python-gui-2/view.qml: Failed to load dependencies for module "QtQuick" version 6.0
我验证了文件没有丢失,它在那里:
C:\github\aorcl\python-gui-2\venv\lib\site-packages\PySide6\qml\QtQml\WorkerScript\workerscriptplugin.dll
我还缺少什么?
解决方法
我在使用 Python 3.9.1 + Pipenv + Windows 10 + Powershell 时遇到了同样的错误。可能跟系统PATH环境变量有关。
然后我尝试使用 venv 和 CMD(不是 Powershell),但使用 .env\Scripts\activate.bat
而不是 .\env\Scripts\Activate.ps1
激活环境。它正在工作
我的环境是 enum RootView {
case A,C
}
struct ContentView : View {
@State private var rootView : RootView = .A
var body: some View {
NavigationView {
switch rootView {
case .A:
ViewA(rootView: $rootView)
case .C:
ViewC(rootView: $rootView)
}
}.navigationViewStyle(StackNavigationViewStyle())
}
}
struct ViewA : View {
@Binding var rootView : RootView
var body: some View {
VStack {
Text("View A")
NavigationLink(destination: ViewB(rootView: $rootView)) {
Text("Navigate to B")
}
}
}
}
struct ViewB : View {
@Binding var rootView : RootView
var body: some View {
VStack {
Text("View B")
NavigationLink(destination: ViewC(rootView: $rootView)) {
Text("Navigate to C")
}
Button(action: {
rootView = .C
}) {
Text("Navigate to C as root view")
}
}
}
}
struct ViewC : View {
@Binding var rootView : RootView
var body: some View {
VStack {
Text("View C")
switch rootView {
case .A:
Button(action: {
rootView = .C
}) {
Text("Switch this to the root view")
}
case .C:
Text("I'm the root view")
}
}
}
}
+ Python3.8
+ PySide6
in Pycharm
。
我将 win10
更改为 import QtQuick 2.0
。它正在工作。