Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Continuously reading the change of a variable happens in one qml file in another qml file



  • Hi, I am new to QML and have this problem:

    I have two QML files, Curve.qml, and the other one is Screen.qml

    The Curve.qml has a graph in which I want to detect if there are any changes happened in the curve or not, I mean if the user increased or decreased any value in the curve. The code in Curve.qml is:

    Curve.qml

    Rectangle {
        id: curvepage
    
        width: 320
        height: 240
        property bool modified: false // initially no changes happened in the curve
        // I want to check when modified is turned to true and read this change in Screen.qml 
    
    
             if (button === "up" || button === "down"  )
            {
                curvepage.modified = true;
            }
    

    In Screen.qml, it is another screen in which I want to detect the change of (modified), if it turns True that means the curve has been edited by a user. the Screen.qml is:

    Screen.qml

    ScreenBase {
        id: root
            if (button === "1"){ // a button when i press, what it does is either to show a popup warning that no changes in the curve have been done, or to activate a button without any warnings if changes happened to the curve.
    
                if (functions.func1) { // this is just to check if the function is set or not 
                    if(curvepage.modified ===false){
                        showNotice = true // this shows a popup that no changes happened in the curve
    
                    } else if(curvepage.modified === true) {
                        functions.functionPressed; // a button is pressed and shown pressed on screen and no popup warnings are shown
                }
    
                }
            }
    
        Curve {
    
            id: curvepage
            modified: false
    
    
    }
    

    thhe problem is that it usually read the set value of (modified) in curve in Screen.qml file, for example if i changed this value to True, it will actiavte the button and will not show popup window, I want to check this change from Curve.qml


  • Qt Champions 2017

    I want to check this change from Curve.qml

    Your question and last statement confused me. What I understand is that you would like to 1. change the variable in Curve.qml.
    2. Check this change in Screen.qml
    3. Based on the value you would like to show the popup.

    Hope this is the correct problem statement.
    Now you are already creating the Curve object inside the Screen.qml. Check onModifiiedChange signal inside the curvepage in Screen.qml. Based on this you take the appropriate action.



  • This post is deleted!


  • This post is deleted!


  • @dheerendra

    Hi, thank you for your reply. Yeah what I want is:

    1- When Up or Down buttons in Curve.qml is pressed, (modified) changes from false to true (that already happens correctly in Curve.qml)

    2- I want to check this change in Screen.qml, i mean to check that modifeid is changed to true.
    3- Based on this (modifeid is changed or not) i will take an action

    I tried this:

    I tried to add this part in curvepage in Screen.qml, but did not work as i want.

    Screen.qml

            id: curvepage
            modified: false
            onModifiedChanged:{ if(curvepage.modified ===false){
                                   curvepage.modified = true
                               }
    

    and also i added to Curve.qml

    Curve.qml

        signal modifiedChanged
    
    

    But it seems i do something wrong


  • Qt Champions 2017

    ok. No need to add modifiedChanged signal. When you declare any property it will have signal defined already. In your case modified is the property added. So it will have signal called modifiedChanged in Curve.qml

    You should just call in Screen.qml

    Curve {
             onModifiedChanged :{
                    console.log(modified)
             }
    }
    

    This should work for you. Just check. If not I will give you sample program shortly.



  • @dheerendra

    Thanks again, but it did not work with me, I am not sure if this maybe a reason or not but in Screen.qml, the if condition is under onMainButtonPressed like this

    Screen.qml

    ScreenBase {
        id: root
    
           onMainButtonPressed: {
    
            if (button === "1"){ // a button when i press, what it does is either to show a popup warning that no changes in the curve have been done, or to activate a button without any warnings if changes happened to the curve.
    
                if (functions.func1) { // this is just to check if the function is set or not 
                    if(curvepage.modified ===false){
                        showNotice = true // this shows a popup that no changes happened in the curve
    
                    } else if(curvepage.modified === true) {
                        functions.functionPressed; // a button is pressed and shown pressed on screen and no popup warnings are shown
                }
    
                }
            }
    
    }
    
    
    will this cause a problem when i call (modified) from Curve object on the same Screen.qml?
    
    It looks like it only reads the modified value that i set initially, I mean if in  Curve object I do like this:
    
    
        id: curvepage
        modified: true
        onModifiedChanged :{
                console.log(modified)
         }
    

    }

    
    it will perform the other option when modifeid is true

  • Qt Champions 2017

    Check the following sample. Based on this you should be able to make your changes.

    ======Screen.qml =====
    import QtQuick 2.11
    import QtQuick.Window 2.11
    
    Window {
        visible: true
        width: 640
        height: 480
        title: qsTr("Hello World")
        MyCurve{
            id : curve
            onModifiedChanged: {
                console.log(modified)
            }
        }
    }
    
    ============MyCurve.qml ==========
    
    import QtQuick 2.0
    import QtQuick.Controls 2.2
    
    Rectangle {
        id : top
        width: 400;height: 400;color: "blue"
        property bool modified : false
        Row {
            Button{
                text :"True"
                onClicked: {
                   top.modified=true;
                }
            }
            Button{
                text :"False"
                onClicked: {
                   top.modified=false;
                }
            }
        }
    }
    

  • Qt Champions 2017

    @mamoud is the issue resolved ? Do you need help ?



  • @dheerendra

    Hi, thank you for this, but I still have the problem. modified state is not changed in Screen.qml file.

    Here I can show you more detailed code, maybe i have another problem that cause this.

    In Curve.qml

    Rectangle {
        id: curvepage
    
        width: 320
        height: 240
        property bool modified: false // initially no changes happened in the curve
        // I want to check when modified is turned to true and read this change in Screen.qml 
    
    
             if (button === "up" || button === "down"  )
            {
                curvepage.modified = true;
            }
    

    I guess no prolem here. But in Screen.qml, I have this:

    Screen.qml

    ScreenBase { // object from anothe qml file called **ScreenBase.qml**
        id: root
    
           onMainButtonPressed: {// from ScreenBase.qml file
    
            if (button === "1"){ // a button when i press, what it does is either to show a popup warning that no changes in the curve have been done, or to activate a button without any warnings if changes happened to the curve.
    
                if (functions.func1) { // this is just to check if the function is set or not 
                    if(curvepage.modified ===false){
                        showNotice = true // this shows a popup that no changes happened in the curve
    
                    } else if(curvepage.modified === true) {
                        functions.functionPressed; // a button is pressed and shown pressed on screen and no popup warnings are shown
                }
    
                }
            }
    
    Curve{ // object is called inside **ScreenBase object** which is written in **Screen.qml file**
    
            id: curvepage
            modified: true
            onModifiedChanged :{
                    console.log(modified)
             }
    }
    
    }
    

    Does this make any problem while calling the variable (modified) from Curve.qml to Screen.qml. should i add anything in ScreenBase.qml??

    The ScreenBase.qml file is a rectangle like this:

    ScreenBase.qml

    Rectangle {
        id: root
        width: 320
        height: 240
        clip: true 
    // and some other buttons definitions
    

    Also to clarify, When i press a butto to change a value on the curve, no matter if i increase or decrease, I just want to check if there is any change, so if i did not press any button, (modified) will be false, if i press any button (modified) will be true

    Thank you very much for your help, but i am abit confused while dealing with those different files



  • @mamoud You have incorrectly initialized curvepage.modified to true in Screen.qml.



  • @Tom_H
    Hi thank you for reply,
    but i have tried to delete it completely without any initalization in Curve object in Screen.qml, but still does not work


Log in to reply