Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Qt Academy Launch in California!

    Unsolved How do I share object between pages of a SwipeView?

    QML and Qt Quick
    swipeview scope
    3
    6
    2502
    Loading More Posts
    • 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.
    • Steve Fallows
      Steve Fallows last edited by

      I am having trouble understanding scope/name resolution in QML. I had the following code in a page of a SwipeView:

      import CTX 1.0
      import QtQuick 2.7
      import QtQuick.Dialogs 1.2
      import Qt.labs.settings 1.0
      
      CalibrationPageForm {
          id: page
          CTPanel {
              id: ct_panel
              Component.onCompleted: {
                  ct_panel.setUp()  // Complete set up of the C++ object
                  console.log("CT Panel component completed.")
              }
              receptorPath: receptorPath_textField.text
          }
          receptorPath_textField.text: settings.receptorPath
      < more code accessing the ct_panel object>
      } 
      

      CTPanel is defined in C++ in the CTX module. This worked, but now I need to access the CTPanel object from another page of the SwipeView. So I moved the CTPanel object up to the ApplicationWindow:

      import CTX 1.0
      import QtQuick 2.7
      import QtQuick.Controls 2.0
      import QtQuick.Layouts 1.0
      
      ApplicationWindow {
          visible: true
          width: 800
          height: 600
          title: qsTr("Application")
          id: app_window
      
          //property alias ct_panel: ct_panel
          
          SwipeView {
              id: swipeView
              anchors.fill: parent
              currentIndex: tabBar.currentIndex
      
              CalibrationPage {
              }
      
              AcquisitionPage {
              }
      
              PatientPage {
              }
      
              UtilsPage {
              }
          }
      
          CTPanel {
              id: ct_panel
              Component.onCompleted: {
                  ct_panel.setUp()  // Complete set up of the C++ object
                  console.log("CT Panel component completed.")
              }
          }
      < page indicator for SwipeView etc. >
      }
      

      Then tried to access it from the calibration page like this:

      CalibrationPageForm {
          id: page
          parent.ct_panel.receptorPath: receptorPath_textField.text
          
          receptorPath_textField.text: settings.receptorPath
      <remainder unchanged>
      }
      

      The id 'ct_panel' is not recognized in the CalibrationPageForm, no matter what I try. I've tried many variations of parent levels, id's and defining the property seen above in the ApplicationWindow, putting the CTPanel in the SwipeView etc.

      How do I define the CTPAnel object so I can access it from both the CalibrationPage and the AcquisitionPage? And how do I refer to it in those pages?

      Possibly my problem here is that I don't quite get the relationship of the Page and PageForm objects (Created by QtDesigner).

      1 Reply Last reply Reply Quote 0
      • P
        peteritv last edited by

        @Steve-Fallows
        Have you tried to put BOTH the property alias AND the CTPanel{} inside the SwipeView{}?

        1 Reply Last reply Reply Quote 0
        • Steve Fallows
          Steve Fallows last edited by

          I had not tried that, so I just did. I still get 'Cannot assign to non-existent property "ct_panel"' whether it use just 'ct_panel', 'parent.ct_panel' or 'swipeView.ct_panel' in CalibrationPageForm.

          Julien B 1 Reply Last reply Reply Quote 0
          • Julien B
            Julien B @Steve Fallows last edited by Julien B

            Hello @Steve-Fallows,

            You can access the data of another Item by its id, nevertheless you cannot access property from another item the way you did. I think you need to use JavaScript via signal handler.

            for example (assuming receptorPath_textField is also an id):

            onClicked: {
                ct_panel.receptorPath = receptorPath_textField.text;
            }
            

            What is the behaviour you are looking for? On which event do you want to change properties from ct_panel?

            Steve Fallows 1 Reply Last reply Reply Quote 0
            • Steve Fallows
              Steve Fallows @Julien B last edited by

              @Julien-B I'm just trying to get the same behavior with the ct_panel object up one level from where it was. I want to bind the receptorPath property to the text field on the CalibrationPage. I'd prefer not to deal explicitly with the events that change it.

              It seems to me the moving the location of ct_panel up should merely necessitate a change in how I refer to it. But it seems that something about the SwipeView or the CalibrationPageForm.ui changes the scope/naming resolution. I'd like to understand what/why that is.

              Julien B 1 Reply Last reply Reply Quote 0
              • Julien B
                Julien B @Steve Fallows last edited by Julien B

                @Steve-Fallows,

                Just to clarify is receptorPath_textField also an id?

                in that case your code

                CTPanel {
                    id: ct_panel
                    Component.onCompleted: {
                        ct_panel.setUp()  // Complete set up of the C++ object
                        console.log("CT Panel component completed.")
                    }
                    receptorPath: receptorPath_textField.text
                }
                

                should work wherever it is placed (in the same qml file).

                if receptorPath_textFieldis is a grouped property from CalibrationPageForm and the id of the page is page

                Have tou tried?

                CalibrationPageForm {
                    id: page
                    receptorPath_textField.text: settings.receptorPath
                }
                CTPanel {
                    id: ct_panel
                    Component.onCompleted: {
                        ct_panel.setUp()  // Complete set up of the C++ object
                        console.log("CT Panel component completed.")
                    }
                    receptorPath: page.receptorPath_textField.text
                }
                
                1 Reply Last reply Reply Quote 0
                • First post
                  Last post