Home > Back-end >  QML Connections target: refer to Connections parent
QML Connections target: refer to Connections parent

Time:09-29

I have a qml prototype object ChargeBar defined in ChargeBar.qml. In main project I instance it two times with different ids. I need a slot in Chargebar to send data to. Now I need to write id of target manually in Connections{target: id...} for every instance. Is there any possibility to connect to target automatically?

# 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 <-- entrance

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

CodePudding user response:

You just have to use the llc object as a target:

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()
}
  • Related