【问题标题】:ReferenceError in qt quick controls tabviewqt快速控件tabview中的ReferenceError
【发布时间】:2014-03-22 13:31:42
【问题描述】:

我写了一个使用 TabView 的 QT Quick 程序。当我单击 Tabview 中的按钮 b1 时,程序应该调用 show_text() 并打印 b1 的文本,但它会打印“ReferenceError: b1 is not defined”。任何建议将不胜感激,谢谢。

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

【问题讨论】:

    标签: qt qml qt-quick tabview qtquickcontrols


    【解决方案1】:

    将对象作为参数传递

    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】:

      您可以通过此示例访问您的对象。

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

      }

      【讨论】:

      • 使用 qt 5.7 我得到Invalid alias reference. Unable to find id "b1"
      猜你喜欢
      • 1970-01-01
      • 2018-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-07
      相关资源
      最近更新 更多