我正在尝试使用PyQt5 + QML,如下所示:
test.py
:
# -*- coding: utf-8 -*-
import sys, os, math
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.Qt import *
from PyQt5.QtQuick import *
class Hello(QObject):
def get_greeting(self):
return QString('Hello World!')
app = QApplication(sys.argv)
hello = Hello()
view = QQuickView()
context = view.rootContext()
context.setContextProperty('hello', hello)
view.setSource(QUrl(__file__.replace('.py', '.qml')))
view.show()
app.exec_()
test.qml
:
import QtQuick 2.0
Rectangle {
color: "red"
width: 350
height: 350
Text {
anchors.centerIn: parent
font.pointSize: 32
color: "white"
text: hello.get_greeting
}
}
它确实弹出一个红色的窗口,但没有任何文本和控制台给我一个消息:
"Qt Warning - invalid keysym:dead_actute"
file:///....../test.qml:11:15: Unable to assign [undefined] to QString
答案 0 :(得分:0)
我找到了解决方案:在@pyqtProperty(str)
之前添加def get_greeting(self):
装饰器。