QML signal and slot between 2 different QML

I need to perform some actions in let's say main.qml according to button press in button.qml. Button inside of the button QML is inside of some custom object. Let's give it a name customObject. So customObject in button.qml looks like this:

customObject {
id:trialObject
signal backPagePressed()

Button {
id:button1
MultitouchArea {
onPressed:
{
   trialObject.backPagePressed()
}

Now when I press the button, it emits backPagePressed(). My question is: How can I make a slot for this signal in main QML? I'm familiar to signal and slot mechanism in C++, but that does not work for QML. I made something like this in main.qml:

Loader 
{
  id:pageLoader
  onBackPagePressed:
{
  pageLoader.source =""
}
}

That part needs to delete the Loader's source so that it will go back to page before. However, I'm not sure about onBackPagePressed: ... How can I connect my signal backPagePressed, to the related part in my loader?

Upvotes: 0

Views: 5060

Answers (1)

Amfasis
Amfasis

Reputation: 4208

You should use a Connections object (documented here) together with the item property of the Loader:

Loader {
    id: pageLoader 
    source: "CustomObject.qml"
}

Connections {
    target: pageLoader.item

    //Qt < 5.15
    onBackPagePressed: pageLoader.source = ""

    //Qt >= 5.15
    function onBackPagePressed()
    {
        pageLoader.source = ""
    }
}

Upvotes: 1

Related Questions