How do I share object between pages of a SwipeView?



  • 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).



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



  • 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.



  • 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?



  • @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.



  • @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
    }
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.