QML连接目标:参考连接父级

2024-05-16 20:05:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我在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


Tags: fromimportselfidosmainsysqml
1条回答
网友
1楼 · 发布于 2024-05-16 20:05:21

您只需将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()
}

相关问题 更多 >