发出Python列表信号到QML界面

huangapple go评论63阅读模式
英文:

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(&quot;HELLO from python &quot;)
            test = [&quot;a&quot;, &quot;b&quot;, &quot;c&quot;]
            self.getListTest.emit(test)
    
    
    if __name__ == &#39;__main__&#39;:
        app = QGuiApplication([])
        engine = QQmlApplicationEngine()
        signal_emitter = PythonSignalEmitter()
    
        engine.rootContext().setContextProperty(&quot;signalEmitter&quot;, signal_emitter)
        engine.load(&quot;main.qml&quot;)
        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(&quot;Hello World&quot;)
    width: 640
    height: 480
    visible: true

    Frame {
        anchors.centerIn: parent
        RowLayout {
            Button {
                text: qsTr(&quot;Quit&quot;)
                onClicked: Qt.callLater(Qt.quit)
            }
            Button {
                text: qsTr(&quot;Test&quot;)
                onClicked: signalEmitter.callList()
            }
        }
    }

    Connections {
        target: signalEmitter
        function onGetListTest(res) {
            console.log(`------------`);
            console.log(`qml list is ${res}`);
            console.log(`------------`);
        }
    }
}

huangapple
  • 本文由 发表于 2023年2月8日 16:28:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/75383045.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定