Jeggu
Jeggu

Reputation: 579

ReferenceError while trying to call function of Item within Tab

I tried to call functions of qml file from another qml file user component id but i am facing some issues. could some one help me out of this. here is my code.

Browser.qml:

import QtQuick 2.0

Item {
    function callme(message) {
        console.log(message)
    }
}

main.qml:

import QtQuick 2.3
import QtQuick.Controls 1.0
import QtQuick.Controls.Styles 1.0

ApplicationWindow {
    visible: true
    width: 640
    height: 100
    TabView {
        id: tabView
        width: 640
        height: 50
        Tab {
            width: 100
            title: "Sample1.html"
            onVisibleChanged: {
                browser1.callme("hi")
            }
            Browser {
                id: browser1
            }
        }
        Tab {
            width: 100
            title: "Sample2.html"
            onVisibleChanged: {
                browser2.callme("bye")
            }

            Browser {
                id: browser2
            }
        }
    }
}

Error reported:

ReferenceError: browser1 is not defined

Upvotes: 0

Views: 575

Answers (1)

Jairo
Jairo

Reputation: 886

If you want access to items inside Tab control, you have to use its item property. I have changed your signal handler and it works:

...
onVisibleChanged: {
    item.callme("hi")
}

Browser{
  id: browser1
}
...

Tab control inherits from Loader component. It takes its children as delegate and they are only created with the tab is activated. Most of the behavior is the same then the Loader component.

Experimentation for the record

What happend if we define two or more components inside a Tab? Loader component only accepts a delegate and the component created is accessed by item property. Tab component maps children property to delegate and you can define more than one, but I realized that only the last child is created.

Upvotes: 1

Related Questions