0

I don't found my answer in my issue , so i would like to explain.

I have some QML pages, i manage my pages with a StackView object and i would like to call a function that is at page 1 from page 2 ... For example

//Page 1
Item{
id: page1
function test(){
   console.debug("Page 1 man ! ");
}
... something 
}
}

I would like to call a test function that is on page 1

//Page 2
Item{
id: page2
Compoment.onCompleted: page1.test();
... something 
}
}

Anyone have a solution ?

yekmen

yekmen
  • 127
  • 9

1 Answers1

0

You cannot directly access function from a page at another one due to a scope limitation. So solution is to create some proxy function in root item. Small example:

Item {
    id: root
    function proxy(target_page) {
        switch(target_page) {
            case 1: page1.test(); break;
            case 2: page2.test(); break;
            case 3: page3.test(); break;
        }
    }
    Item {
        id: page1
        function test() {
        }
        Component.onCompleted: {
            root.proxy(2);
        }
    }
    Item {
        id: page2
        function test() {
        }
    }
    Item {
        id: page3
        function test() {
        }
    }

}

This is not live example, just idea. Here page1 calls page2 on its initialization through root element function.

folibis
  • 12,048
  • 6
  • 54
  • 97