QML Connections 目标:参考 Connections parent

QML Connections target: refer to Connections parent

我在 ChargeBar.qml 中定义了一个 qml 原型对象 ChargeBar。在主项目中,我用不同的 ID 实例化了两次。我需要 Chargebar 中的插槽来发送数据。现在我需要在 Connections{target: id...} 中为每个实例手动写入目标 ID。有没有可能自动连接到目标?

# This Python file uses the following encoding: utf-8
import os, sys, random
from pathlib import Path
from PySide2.QtGui import QGuiApplication
from PySide2.QtQml import QQmlApplicationEngine
from PySide2.QtCore import QObject, Signal, Slot
APPLICATIONDATA = os.path.join(os.getenv('APPDATA'), "DSController")

class LowLevelControler(QObject):
    def __init__(self):
        QObject.__init__(self)

    nextNumber = Signal(int)
    @Slot()
    def start(self):
        print("giveNumber")
        self.nextNumber.emit(random.randint(0, 99))

if __name__ == "__main__":

    SERIAL_LLC_RIGHT = "0672FF485550755187034646"
    SERIAL_LLC_LEFT = "066AFF575256867067063324"

    app = QGuiApplication(sys.argv)
    engine = QQmlApplicationEngine()

    LLC = dict()
    LLC["right"] = LowLevelControler()
    LLC["left"] = LowLevelControler()
    
    engine.rootContext().setContextProperty("llcRight", LLC["right"])
    engine.rootContext().setContextProperty("llcLeft", LLC["left"])

    engine.load(os.fspath(Path(__file__).resolve().parent / "main.qml"))

    if not engine.rootObjects():
        sys.exit(-1)

    sys.exit(app.exec_())

main.py <-- 入口

import QtQuick 2.12
import QtQuick.Controls 2.2
import QtQuick.Window 2.12

Window {
    id: window
    width: 1080
    height: 1920
    //visibility: "FullScreen"
    visible: true

ChargeBar{
    id: drawerL
    edge: Qt.LeftEdge
    llc: llcLeft
}

ChargeBar{
    id: drawerR
    edge: Qt.RightEdge
    llc: llcRight //llcRight=Instance of Python class
            
    Component.onCompleted: llc.nextNumber.connect(reNextNumber)
    
    Connections {
        target: drawerR  
// PROBLEM: I have to insert target id for every instance manually, 
// so I cannot put this passage to ChargeBar.qml */
        function onReNextNumber(number) {
            print(number)
            print("emitted")
            }
        }
    }
    Component.onCompleted: drawerR.open()
}

main.qml

import QtQuick 2.12
import QtQuick.Controls 2.12

Drawer {
    x:0
    y:0

    property var llc
    signal reNextNumber(int number)

    width: 0.66 * window.width
    height: window.height

// I want to define Connections here

    Button{
        id: button
        anchors.centerIn: parent
        height: 320
        width: 320
        onClicked: {
            llc.start()
        }
    }
}

ChargeBar.qml

您只需使用 llc 对象作为目标:

ChargeBar.qml

import QtQuick 2.12
import QtQuick.Controls 2.12

Drawer {
    id: root

    property var llc

    signal reNextNumber(int number)

    width: 0.66 * window.width
    height: window.height

    Button {
        id: button
        anchors.centerIn: parent
        height: 320
        width: 320
        onClicked: llc? llc.start(): null
    }
    Connections {
        target: llc
        function onNextNumber(n) {
            root.reNextNumber(n)
        }
    }
}

main.qml

import QtQuick 2.12
import QtQuick.Controls 2.2
import QtQuick.Window 2.12

Window {
    id: window
    width: 1080
    height: 1920
    visible: true

    ChargeBar {
        id: drawerL
        edge: Qt.LeftEdge
        llc: llcLeft
    }
    ChargeBar {
        id: drawerR
        edge: Qt.RightEdge
        llc: llcRight
        // for testing
        onReNextNumber: function(number){
            console.log("Test", number)
        }
    }
    Component.onCompleted: drawerR.open()
}