PySide Signal argument can#39;t be retrieved from QML(无法从 QML 中检索 PySide Signal 参数)
问题描述
我注意到 QML 可以使用 Connections 对象接收 Python 发出的信号.不幸的是,我不知道如何让该对象接收该信号的参数.
I've noticed that QML can receive a signal emitted from Python by using the Connections object. Unfortunately, I can't figure out how to get that object to receive the arguments of that signal.
我创建了一个最小的测试用例来演示我想要做什么:
I've created a minimal test case that demonstrates what I want to do:
min.py
from PySide import QtCore, QtGui, QtDeclarative
import sys
# init Qt
app = QtGui.QApplication(sys.argv)
# set up the signal
class Signaller(QtCore.QObject):
emitted = QtCore.Signal(str)
signaller = Signaller()
# Load the QML
qt_view = QtDeclarative.QDeclarativeView()
context = qt_view.rootContext()
context.setContextProperty('signaller', signaller)
qt_view.setResizeMode(QtDeclarative.QDeclarativeView.SizeRootObjectToView)
qt_view.setSource('min.qml')
qt_view.show()
# launch the signal
signaller.emitted.emit("Please display THIS text!")
# Run!
app.exec_()
还有 min.qml
import QtQuick 1.0
Rectangle {
width:300; height:100
Text {
id: display
text: "No signal yet detected!"
Connections {
target: signaller
onEmitted: {
display.text = "???" //how to get the argument?
}
}
}
}
推荐答案
从 Qt 4.8 开始,PySide 根本不处理信号参数名称.
As of Qt 4.8, PySide doesn't handle signal parameter names at all.
但是您可以使用命名参数创建一个 QML 信号并使用 Javascript 将您的 python 信号连接到它:
But you can create a QML signal with named parameters and connect your python signal to it using Javascript:
import QtQuick 1.0
Rectangle {
width:300; height:100
Text {
id: display
text: "No signal yet detected!"
signal reemitted(string text)
Component.onCompleted: signaller.emitted.connect(reemitted)
onReemitted: {
display.text = text;
}
}
}
这篇关于无法从 QML 中检索 PySide Signal 参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:无法从 QML 中检索 PySide Signal 参数
基础教程推荐
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 筛选NumPy数组 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01