问题
I have some experience with Python console applications and now trying to start with Qt for Python (Qt 5.12, PySide2). Actually I'm trying some basic tutorials to understand how it should work.
So, I created very simple view.qml:
import QtQuick 2.0
import QtQuick.Controls 2.13
ApplicationWindow {
visible: true
Button {
id: button
text: qsTr("ClickOnMe")
}
Connections {
target: button
onClicked: con.say_hello()
}
}
and have following python code to work with it:
from PySide2.QtCore import QObject, Slot
from PySide2.QtGui import QGuiApplication
from PySide2.QtQml import QQmlApplicationEngine
class Bridge(QObject):
@Slot()
def say_hello(context):
print("Button clicked, Hello!", context)
app = QGuiApplication([])
engine = QQmlApplicationEngine()
bridge = Bridge()
context = engine.rootContext()
context.setContextProperty("con", bridge)
engine.load("view.qml")
engine.rootObjects()
exit(app.exec_())
It works fine when I simly run it - i.e. I see an application window with a button. I may click a button and see message printed in console.
But when I try to debug it - it stops immediately on line class Bridge(QObject):
with following exception:
<class 'tuple'>: (<class 'TypeError'>, TypeError("'Shiboken.ObjectType' object is not iterable"), <traceback object at 0x7fe195b23680>)
I tryied the same in Qt Creator - works fine, not problems with Run and Debug (the minor issue - messages in Qt Creator console appear only after application termination). But I like PyCharm more so would like to understand how I may fix this problem and continue to use PyCharm with Qt. If it's important - I'm running ArchLinux.
回答1:
A few days ago Arch Linux updated its version of Python to 3.8 but PySide2 does not yet have a compatible version causing the error you are pointing out. As they point out in this report PYSIDE-1140:
I'm keeping this open, but it will probably be solved for 5.14, since then Python 3.8 will be introduced as a new compatible Python version.
So you have 2 options:
Wait for a release of PySide2 that is compatible with Python3.8.
Or install Python3.7 or an old version from aur (using yay) and then install PySide2 using pip.
回答2:
This was actually a bug in Qt for Python using Python 3.8, however it is already been solved here.
Just update your qt:
pip install PyQt5
来源:https://stackoverflow.com/questions/58904472/pycharm-fails-to-debug-qt5-pyside2-code-error-shiboken-objecttype-object-i