Sharing a singleton timer between two different qmls

Viewed 29

I am trying to use a pragma SingleTon timer between two different qml's.

I wanted to achieve two expectations

  1. Creating a singleton timer (countdown timer) and share it between to qml's.
  2. Transferring the left over countdown value from one qml (Actpopup.qml) to other qml (Body.qml).

I have tried in the below way

main.qml

import QtQuick 2.9
import QtQuick.Window 2.2

import "qrc:///timerFile"

Window {
    visible: true
    width: 800
    height: 1200
    title: qsTr("Hello World")

    Body{

    }

    SosPopupActivationReq{

    }
    
    TonTimerSingle{
        /*i think one error related to singleton is because of this syntax*/
    }
}

Body.qml

Item {
id: root
Button{
                id: qoBtn
                implicitWidth: 162
                implicitHeight: 162
                text: "QO"
                background: Rectangle{
                    id:sosBtnBg
                    color: countdownTimer.running ?  "#cc0000" : "#bfc6d0"
                }
                contentItem: Item{
                    ColumnLayout{
                        anchors.fill: parent
                        Image{
                            width:50
                            height: 50
                            source: (!qoActive) ? "images/rectangle-1694.png" : "images/oval-1694.png"
                            Text{
                                text:qsTr("Hi")
                                anchors.centerIn: parent
                            }
                            Layout.alignment: Qt.AlignHCenter | Qt.AlignVCenter
                        }
                        Text{
                            text: qoBtn.text
                            Layout.alignment: Qt.AlignHCenter | Qt.AlignVCenter
                        }
                    }
                }
                onClicked: {
                    console.error("======> Clicked <========")
                    var popupComponent = Qt.createComponent("Actpopup.qml")
                    if( popupComponent.status !== Component.Ready )
                    {
                        if( popupComponent.status === Component.Error )
                            console.debug("Error:"+ popupComponent.errorString() );
                        return; // or maybe throw
                    }
                    var popup2 = popupComponent.createObject(qoBtn);
                    popup2.open()
                }
            }
}

Actpopup.qml

 Popup{
    id: actPopup
    
          RoundButton{
                id:closeBtn
                text: "X"
                x: 55
                y: 46
                width: 40
                height: 40
                onClicked: {
                    /*Wanted to send intimation to body that Act popup got closed and left over
            count down value to be display on button*/
                    console.error("Closed the POPUP==========>")
                }
            }
    
            Slider{
                id:control
                value: 0
                Layout.topMargin: 20
                Layout.leftMargin: 154
                onPositionChanged: {
                    backgroundText.opacity = 1-position
                    if(control.visualPosition === 1.0)
                    {
                        console.error("POP Activated====>")
                /* when the user drags the slider handle till the end,
                        i want to send a trigger to start the single ton timer*/    
                        console.error("SOS Activated====>")
                    }
                }
    
                background:  Rectangle{
                    x: control.leftPadding
                    y: control.topPadding + control.availableHeight / 2 - height / 2
                    implicitWidth: 331
                    implicitHeight: 68
                    width: control.availableWidth
                    height: implicitHeight
                    radius: 10
                    color: '#E7CACC'
                    border.width: 1
                    border.color: "#000000"
    
                    Rectangle{
                        width: control.visualPosition * parent.width
                        height: parent.height
                        color: "#cc0000"
                        radius: 10
                    }
    
                    Text{
                        id:backgroundText
                        text: "Slide to Send"
                        anchors.left: parent.left
                        anchors.leftMargin: 200
                        font.pixelSize: 22
                        color: "#ffffff"
                        anchors{
                            centerIn: parent
                        }
                    }
                }
    
                handle: Rectangle {
                    id: sliderHandle
                    property int fnValu : control.leftPadding  + control.visualPosition * (control.availableWidth- width)
                    x: control.leftPadding  + control.visualPosition * (control.availableWidth- width)
                    y: control.topPadding + control.availableHeight / 2 - height / 2
                    implicitHeight: 70
                    implicitWidth: 88
                    radius: 10
    
                    Text{
                        anchors.centerIn: parent
                        text: "ACT"
                        font.pixelSize: 22
                        color: '#ffffff'
                    }
                    gradient: Gradient {
                        GradientStop { position: 1.0; color: '#990000' }
                        GradientStop { position: 0.0; color: '#cc0000' }
                    }
                }
            }
Label{/* To display the countdown timer value*/
                id:popupTimerLabel
                width: 428
                text: "SOS will send in "+popupCountdown+"Seconds"
                color: "#cc0000"
                font.pixelSize: 40
                Layout.alignment: Qt.AlignVCenter | Qt.AlignLeft
            }

}

TonTimerSingle.qml

pragma Singleton

import QtQuick 2.9

Timer {
    id: root

    interval: 1000
    running: countdown > 0
    repeat: true

    signal quietModeTriggered

    onTriggered:  {
        console.error("Countdown Value=====>", popupCountdown)
        countdown --;
        quietModeTriggered()
        console.error("==========TimerTriggered")
    }
}

I have followed the link and example to create a single ton qml timer http://imaginativethinking.ca/make-qml-component-singleton/ and created a qmldir with following lines singleton TonTimer 1.0 TonTimerSingle.qml but facing the following errors in various instances --> :-1 pragma Singleton used with a non composite singleton type SingleTonTimer --> Composite Singleton Type TonTimer is not creatable.

And firing a signal from ActPopup when popup close button is pressed, i guess the popup is getting destroyed even before the signal is fired or reached the other qml.

Any help here would be appreciated.

Thanks in advance !!

0 Answers
Related