ReferenceError in qt quick controls tabview

不问归期 提交于 2019-12-19 11:24:15

问题


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()
                }
            }
        }
    }
}

回答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)
                }
            }
        }
    }
}



回答2:


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()
            }
        }
    }
}

}



来源:https://stackoverflow.com/questions/21927587/referenceerror-in-qt-quick-controls-tabview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!