2014-09-04 31 views
7

访问QML项目,我开始学习QML,我发现了以下错误:不能由ID内SPLITVIEW

ReferenceError: chatTextArea is not defined

我有一个全局函数,它在同一个QML内的项目的东西文件,由id。

出于某种原因,我无法通过我的TextArea的ID或SplitView中的任何项目进行访问。但是我能够操纵TabView和每个Tab的属性。

我断码:

import QtQuick 2.2 
import QtQuick.Controls 1.1 
import QtQuick.Layouts 1.1 

Rectangle { 
id: lobby 

function appendChatMsg(msg) { 
    chatTextArea.append(msg) //causes: ReferenceError: chatTextArea is not defined 
} 

TabView { 
    id: frame 

    Tab { //I CAN access this item via ID. 
     id: controlPage 

     SplitView { 
      anchors.fill: parent 

      TableView { 
       Layout.fillWidth: true 
      } 

      GridLayout { 
       columns: 1 

       TextArea { //This item I CANNOT access via ID. 
        id: chatTextArea 

        Layout.fillHeight: true 
        Layout.fillWidth: true 
       } 

       TextField { 
        placeholderText: "Type something..." 
        Layout.fillWidth: true 
       } 
      } 
     } 
    } 
} 
} 

任何想法,为什么chatTextArea超出了我的函数的范围是什么?提前致谢。

回答

3

更改代码的开始部分,以水木清华这样的:

import QtQuick 2.2 
import QtQuick.Controls 1.1 
import QtQuick.Layouts 1.1 

Rectangle { 
id: lobby 

function appendChatMsg(msg) { 
    controlPage.chatArea.append(msg) //causes: ReferenceError: chatTextArea is not defined 
} 

TabView { 
    id: frame 

    Tab { //I CAN access this item via ID. 
     id: controlPage 
     property Item chatArea: item.chatArea 

     SplitView { 
      property Item chatArea: chatTextArea 

原因这部作品,是Tab原来表现得像一个Loader(每文档),装载你给哪个组件它;因此,代码中的SplitView是一个组件规范,并且该组件在单独的QML上下文(由文档根项目的父元素构成)中由Tab实例化。这就是为什么在该环境中的所有东西都可以在范围链中看到事物(如appendMessage()函数),但不是相反:)

+0

谢谢!这消除了错误。 但是我仍然有困难。我正在尝试回复“主”QML文件中的信号。我的大厅通过Loader动态加载到main.qml中。当main.qml中的信号被触发时,如何在Lobby.qml中触发我的函数? – jub 2014-09-05 03:09:38

+0

你可以在范围链中通过id访问main,并通过'signalName.connect()'连接到它的信号,我想(可能在大厅的'onCompleted()')。 – mlvljr 2014-09-05 11:40:40