英文:
Emit python list in Signal to Qml ui
问题
我正在尝试使用信号/槽将Python脚本传递到QML界面,虽然对于某些类型(如str)可以正常工作,但当我尝试发射一个列表时似乎不起作用:
**Python:**
```python
from PySide6.QtCore import QObject, Signal, Slot
from PySide6.QtGui import QGuiApplication
from PySide6.QtQml import QQmlApplicationEngine
import time, sys
class PythonSignalEmitter(QObject):
getListTest = Signal(list)
@Slot()
def callList(self):
print("HELLO from python ")
test = ["a", "b", "c"]
self.getListTest.emit(test)
if __name__ == '__main__':
app = QGuiApplication([])
engine = QQmlApplicationEngine()
signal_emitter = PythonSignalEmitter()
engine.rootContext().setContextProperty("signalEmitter", signal_emitter)
engine.load("main.qml")
sys.exit(app.exec())
main.qml文件的片段:
Connections {
target: signalEmitter
function onSignal(res) {
console.log(`------------`);
console.log(`qml list is ${res}`);
console.log(`------------`);
}
}
这个输出只是:
HELLO from python
所以应用程序可以正常运行,单击指定组件后,调用了槽,但是在QML端的日志甚至没有被打印出来,似乎信号甚至没有被发射。
<details>
<summary>英文:</summary>
I am trying to communicate a python script to a qml ui using signal/slot and, while it works for some types (str) it seems to not be working when I try emitting a list:
**Python:**
from PySide6.QtCore import QObject, Signal, Slot
from PySide6.QtGui import QGuiApplication
from PySide6.QtQml import QQmlApplicationEngine
import time, sys
class PythonSignalEmitter(QObject):
getListTest = Signal(list)
@Slot()
def callList(self):
print("HELLO from python ")
test = ["a", "b", "c"]
self.getListTest.emit(test)
if __name__ == '__main__':
app = QGuiApplication([])
engine = QQmlApplicationEngine()
signal_emitter = PythonSignalEmitter()
engine.rootContext().setContextProperty("signalEmitter", signal_emitter)
engine.load("main.qml")
sys.exit(app.exec())
**Fragment of the main.qml file:**
Connections {
target: signalEmitter
function onSignal(res) {
console.log(`------------`);
console.log(`qml list is ${res}`);
console.log(`------------`);
}
}
The output of this just gives:
HELLO from python
So the app runs with no problem, and after a click on a specified component, the slot is called, but the log in the QLM side is not even printed, seems like the signal is not even emitted.
</details>
# 答案1
**得分**: 1
You need to rename `onSignal` with `onGetListTest`. The rest is fine.
```qml
import QtQuick
import QtQuick.Layouts
import QtQuick.Controls
import QtQuick.Window
ApplicationWindow {
id: main
title: qsTr("Hello World")
width: 640
height: 480
visible: true
Frame {
anchors.centerIn: parent
RowLayout {
Button {
text: qsTr("Quit")
onClicked: Qt.callLater(Qt.quit)
}
Button {
text: qsTr("Test")
onClicked: signalEmitter.callList()
}
}
}
Connections {
target: signalEmitter
function onGetListTest(res) {
console.log("------------");
console.log("qml list is " + res);
console.log("------------");
}
}
}
英文:
[EDIT]
You need to rename onSignal
with onGetListTest
. The rest is fine.
import QtQuick
import QtQuick.Layouts
import QtQuick.Controls
import QtQuick.Window
ApplicationWindow {
id: main
title: qsTr("Hello World")
width: 640
height: 480
visible: true
Frame {
anchors.centerIn: parent
RowLayout {
Button {
text: qsTr("Quit")
onClicked: Qt.callLater(Qt.quit)
}
Button {
text: qsTr("Test")
onClicked: signalEmitter.callList()
}
}
}
Connections {
target: signalEmitter
function onGetListTest(res) {
console.log(`------------`);
console.log(`qml list is ${res}`);
console.log(`------------`);
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论