【问题标题】:PyQt5 + QML: Empty window with "Unable to assign [undefined] to QString"PyQt5 + QML:带有“无法将[未定义]分配给QString”的空窗口
【发布时间】:2014-06-05 10:25:56
【问题描述】:

我正在尝试 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 

【问题讨论】:

  • 如果你这样做会发生什么return "Hello World!"
  • 删除QString()后得到同样的警告信息。

标签: qml pyqt5


【解决方案1】:

我找到了解决方案:在def get_greeting(self): 之前添加一个@pyqtProperty(str) 装饰器。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-30
    • 2018-05-30
    • 1970-01-01
    • 2016-07-04
    • 2014-01-18
    • 1970-01-01
    • 2018-08-15
    • 1970-01-01
    相关资源
    最近更新 更多