Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Continuously reading the change of a variable happens in one qml file in another qml file
Forum Update on Monday, May 27th 2025

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

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
12 Posts 3 Posters 3.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mamoud
    wrote on 26 Nov 2018, 12:50 last edited by mamoud
    #1

    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

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dheerendra
      Qt Champions 2022
      wrote on 26 Nov 2018, 13:12 last edited by
      #2

      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.

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      M 1 Reply Last reply 26 Nov 2018, 14:01
      0
      • M Offline
        M Offline
        mamoud
        wrote on 26 Nov 2018, 13:18 last edited by mamoud
        #3
        This post is deleted!
        1 Reply Last reply
        0
        • M Offline
          M Offline
          mamoud
          wrote on 26 Nov 2018, 13:43 last edited by mamoud
          #4
          This post is deleted!
          1 Reply Last reply
          0
          • D dheerendra
            26 Nov 2018, 13:12

            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.

            M Offline
            M Offline
            mamoud
            wrote on 26 Nov 2018, 14:01 last edited by mamoud
            #5

            @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

            1 Reply Last reply
            0
            • D Offline
              D Offline
              dheerendra
              Qt Champions 2022
              wrote on 26 Nov 2018, 14:24 last edited by dheerendra
              #6

              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
              @Community Service
              Certified Qt Specialist
              http://www.pthinks.com

              M 1 Reply Last reply 26 Nov 2018, 14:46
              0
              • D dheerendra
                26 Nov 2018, 14:24

                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.

                M Offline
                M Offline
                mamoud
                wrote on 26 Nov 2018, 14:46 last edited by mamoud
                #7

                @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
                1 Reply Last reply
                0
                • D Offline
                  D Offline
                  dheerendra
                  Qt Champions 2022
                  wrote on 26 Nov 2018, 14:49 last edited by dheerendra
                  #8

                  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;
                              }
                          }
                      }
                  }
                  

                  Dheerendra
                  @Community Service
                  Certified Qt Specialist
                  http://www.pthinks.com

                  1 Reply Last reply
                  0
                  • D Offline
                    D Offline
                    dheerendra
                    Qt Champions 2022
                    wrote on 27 Nov 2018, 02:22 last edited by
                    #9

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

                    Dheerendra
                    @Community Service
                    Certified Qt Specialist
                    http://www.pthinks.com

                    M 1 Reply Last reply 27 Nov 2018, 07:21
                    0
                    • D dheerendra
                      27 Nov 2018, 02:22

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

                      M Offline
                      M Offline
                      mamoud
                      wrote on 27 Nov 2018, 07:21 last edited by mamoud
                      #10

                      @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

                      T 1 Reply Last reply 27 Nov 2018, 20:14
                      0
                      • M mamoud
                        27 Nov 2018, 07:21

                        @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

                        T Offline
                        T Offline
                        Tom_H
                        wrote on 27 Nov 2018, 20:14 last edited by
                        #11

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

                        M 1 Reply Last reply 28 Nov 2018, 08:11
                        0
                        • T Tom_H
                          27 Nov 2018, 20:14

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

                          M Offline
                          M Offline
                          mamoud
                          wrote on 28 Nov 2018, 08:11 last edited by
                          #12

                          @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

                          1 Reply Last reply
                          0

                          4/12

                          26 Nov 2018, 13:43

                          8 unread
                          • Login

                          • Login or register to search.
                          4 out of 12
                          • First post
                            4/12
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved