1

I have written a QT Quick program use TabView. When I click the botton b1 which is in Tabview, the program should call show_text() and print the text of b1, but it print "ReferenceError: b1 is not defined". Any suggestion will be appreciated, thanks.

import QtQuick 2.2
import QtQuick.Controls 1.1
import QtQuick.Window 2.1



ApplicationWindow {
    function show_text() {
        console.log(b1.text)
    }

    TabView {
        id: tv
        Tab {
            id: tab1
            Button{
                id: b1
                text:"b1's text"
                onClicked: {
                    //console.log(b1.text)
                    show_text()
                }
            }
        }
    }
}
davy
  • 123
  • 1
  • 3
  • 8

2 Answers2

1

Pass the object as a parameter

import QtQuick 2.2
import QtQuick.Controls 1.1
import QtQuick.Window 2.1

ApplicationWindow {
    function show_text(myobject) {
        console.log(myobject.text)
    }

    TabView {
        id: tv
        Tab {
            id: tab1
            Button{
                id: b1
                text: "b1's text"
                onClicked: {
                    show_text(b1)
                }
            }
        }
    }
}
Simon Warta
  • 10,850
  • 5
  • 40
  • 78
0

You can access in your object with this example.

ApplicationWindow {
function show_text() {
    console.log(tv.b1Text);
}

TabView {
    id: tv
    property alias b1Text: b1.text
    Tab {
        id: tab1
        Button{
            id: b1
            text:"b1's text"
            onClicked: {
                //console.log(b1.text)
                show_text()
            }
        }
    }
}

}

yekmen
  • 127
  • 9