Qt - How to connect signal from loader item, if loader source changes dynamically
<p>I have an application which needs the screen to be switched between multiple available screens. I am checking if this is possible with loader in qml.</p>

<p>The issue i am facing is connecting signals from loaded item.</p>

<p>I use an application example in qt documentation and found CreateConnection in application qml cannot have if condition. </p>

<p>I also tried to make it signal slot connection in a function and call in on source change of loader, but that too did not work.</p>

<p>Application.qml</p>

<pre><code>import QtQuick 2.0

Item {
    width: 100; height: 100

    Loader {
       id: myLoader
       source: "MyItem.qml"
    }

    Connections {
        target: myLoader.item
        // here i tried using if (myLoader.item == "qrc:MyItemOne.qml") , but can't use if
        onChangeToSecond: {
               myLoader.source = "MyItemTwo.qml"
        }

        onChangeToFirst: {
               myLoader.source = "MyItemOne.qml"
        }
    }
}
</code></pre>

<p>MyItemOne.qml</p>

<pre><code>import QtQuick 2.0

Rectangle {
   id: myItem
   signal changeToSecond()

   width: 100; height: 100

   MouseArea {
       anchors.fill: parent
       onClicked: myItem.changeToSecond()
   }
}
</code></pre>

<p>MyItemTwo.qml</p>

<pre><code>import QtQuick 2.0

Rectangle {
   id: myItem
   signal changeToFirst()

   width: 100; height: 100

   MouseArea {
       anchors.fill: parent
       onClicked: myItem.changeToFirst()
   }
}
</code></pre>

<p>Someone knows any way to use loader for this, or loader should not be used for this?</p>