Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Event when page is rendered/all components are loaded

Tags:

qt

qt5

qml

qtquick2

I have a Qml component consisting of sub-components. After everything has been loaded (rendered) I want to perform some action. Is there a way to find out when all components are loaded? Just using the Component.onCompleted event on the root element does not suffice because the children are not guaranteed to be loaded.

Regards,

like image 818
Hyndrix Avatar asked Nov 08 '15 07:11

Hyndrix


1 Answers

You should be able to use the afterSynchronizing() signal of QQuickWindow to achieve this:

import QtQuick 2.2
import QtQuick.Controls 1.1
import QtQuick.Layouts 1.1

ApplicationWindow {
    visible: true
    width: 1280
    height: 700

    property bool initialised: false

    onAfterSynchronizing: {
        if (!initialised) {
            print("initialising...");
            // stuff...
            initialised = true;
        }
    }
}

Pros:

  • You get it for free.

Cons:

  • You can only use it on QQuickWindow derivatives.

An alternative is to use a Loader; specifically its loaded() signal:

Loader {
    source: "MyComponent.qml"
    onLoaded: {
        // stuff...
    }
}

Pros:

  • A much clearer alternative to anyone who may have to maintain your code.
  • Works without having a Window; can use it at any level in your scene's "hierarchy".

Cons:

  • Comes with a little overhead. If the component is constructed often, at a low level (like a button), it may be worth profiling to see if it has a negative effect on performance. If not, it's negligible.
like image 87
Mitch Avatar answered Sep 21 '22 02:09

Mitch