3

I may be missing something here but I'm not sure why the code is running before page 2 should be loaded.

http://pastebin.ubuntu.com/8068379/

And is there any way to prevent the functions and code within page 2 from running before the button is pressed?

1 Answer 1

3

Component.onCompleted runs as soon as the interpreter has read the QML file and parsed it into objects. Your "page 2" object is created at this time, but it has visible: false so you can't see it. That doesn't keep the onCompleted code from running.

I know of three ways to get code to run only when the page gets pushed to the stack:

  1. Create a custom function that calls pageStack.push() and triggers whatever other code you want run.

  2. Move the code from page 2's onCompleted handler to the onVisibleChanged handler. Note that this will be called both when the page is pushed a popped from the stack; check the value of visible to tell which is happening.

  3. Move the page 2 object into a separate file, say "page2.qml", and change the push call to pageStack.push(Qt.resolvedUrl("page2.qml")). This will create a new page 2 object each time it's pushed to the stack; this may or may not be what you desire.

Note that all three of these will call the code each time page 2 is pushed to the stack. If you only want it to be called the first time, you'll have to keep track of that yourself. For more information on how object creation interacts with the pageStack, check out this answer.

1
  • Number 3 is exactly what I was looking for, I've read it before but I have no idea why it didn't click until you said it! tyvm!
    – ikt
    Aug 19, 2014 at 10:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .