2017-08-02 41 views
0

我有幾個qml文件,每個文件在我的應用程序中定義一個屏幕。有時候,一個屏幕上的操作應該改變不同屏幕上的項目。現在我這樣做是通過設置 物業項目buttonId然後做在不同的qml文件中訪問項目

for (var i = 0; i < buttonId.parent.children.length; i++) { 
    buttonId.parent.children[i].state="inactive" 
} 

是否有可能直接訪問項目/對象在不同的​​文件,例如通過他們的ID?

回答

2

除非您有該qml類型或單例的實例,否則不能訪問「不同的qml文件」中的對象。

通過id訪問只有當試圖訪問它的對象創建在它想通過id訪問的對象範圍內時纔可能。

您有幾個屏幕,只需將它們作爲根qml對象的屬性公開,然後由於動態作用域而可以從整個應用程序訪問它們,除非它們被具有相同名稱的其他屬性所遮蔽。

1

As dtech表示,您不能訪問不同文件中的對象,因爲它們沒有對象。

比方說,你有一個名爲BlueSqare.qml

import QtQuick 2.0 
Rectangle { 
    id: root 
    width: 100 
    height: 100 
    color: 'purple' 
} 

現在你有文件的main.qml

import QtQuick 2.0 
import QtQuick.Controls 2.0 
ApplicationWindow { 
    id: root 
    width: 300 
    height: 300 
    visible: true 
    Row { 
     id: rowdi 
     spacing: 3 
     Repeater { 
      model: 3 
      delegate: BlueSquare {} 
     } 
    } 
} 

你會看到,它創建三個我BlueSquare S的,因此是不確定哪一個我想改變,如果我設置root.color = 'blue' - 或實際上它不是:因爲我有id: rootmain.qml,這是ApplicationWindow的另一個對象,我將它設置爲c olor to blue。

您可以看到:id只需要在一個文件中唯一,因此只能在id之內引用該文件中的對象。

有一個例外:您可以向上移動使用qml文件的文件樹並引用它們的ID。

所以如果你想在我的例子中的BlueSqure中參考rowdi,這將參考安排它們的。這是可能的,因爲您可以有一個明確定義的規則,您試圖通過此id引用該對象。

如果在文件(這裏是:BlueSquare.qml)中找不到id您嘗試引用它,那麼它將查找實例化對象的文件(此處爲main.qml)。如果發現它,搜索結束。如果沒有找到它,它會嘗試移動一個文件 - 這在這裏是不可能的,就像main.qml故事結束一樣。

但是,如果可能,應該避免這種情況。也許你想在其他地方重新使用BlueSquare,並且在那裏你沒有,同時也有id: rowdi。相反,你應該注入參考uppon實例化。

import QtQuick 2.0 
import QtQuick.Controls 2.0 
ApplicationWindow { 
    id: root 
    width: 300 
    height: 300 
    visible: true 
    Row { 
     id: rowdi 
     spacing: 3 
     property color squareColors: 'blue' 
     Repeater { 
      model: 3 
      delegate: BlueSquare { 
       color: rowdi.squareColors 
      } 
     } 
    } 
} 

在這裏,我們我們,我們將它定義在文件中的idrowdi,將數據注入到BlueSquare秒。

您也可以通過它們將QtObject作爲模型保存所有值。這是,如果你要改變這些值特別有用:

// MyModelCountingButton.qml

import QtQuick 2.0 
import QtQuick.Controls 2.0 
Button { 
    property QtObject model // Better would be to be more specific the QtObject and have a object that guarantees to have your properties 
    text: model ? model.count : '-' 
    onClicked: { 
     if (model) model.count++ 
    } 
} 

,然後在main.qml

import QtQuick 2.0 
import QtQuick.Controls 2.0 
ApplicationWindow { 
    id: root 
    width: 800 
    height: 600 
    visible: true 

    QtObject { 
     id: myCountingModel 
     property int count: 0 
    } 

    MyModelCountingButton { 
     id: button1 
     model: myCountingModel 
    } 

    MyModelCountingButton { 
     id: button2 
     model: myCountingModel 
    } 
} 

現在你可以使用普通模式更改會影響其他對象的值,而不會引用自己文件之外的內容,這會提高可重用性。

基本上,你告訴他們應該從哪裏讀取和存儲他們的數據,如果這個地方是你的屏幕共享的,你就贏了。

有關問題的最簡單的方法將是在模型中的currentIndex - 屬性,分配給每一個畫面,然後在文件中的索引

// Screen.qml

[...] 
Item { 
    id: root 
    property int index 
    property QtObject indexObject 
    Binding { 
     target: root 
     property: 'state' 
     value: 'inactive' 
     when: index !== indexObject.currentIndex 
    } 
} 

和在main.qml

[...] 
ApplicationWindow { 
    [...] 
    QtObject { 
     id: indexObject 
     property int currentIndex: 0 
    } 

    Screen { 
     index: 0 
     indexObject: indexObject 
    } 
    Screen { 
     index: 1 
     indexObject: indexObejct 
    } 
    [...] 
} 

或者你有屏幕:

[...] 
Item { 
    id: root 
    property int index 
    property int currentIndex 
    signal turningActive // this must be emitted when you want to have this screen active. 
    Binding { 
     target: root 
     property: 'state' 
     value: 'inactive' 
     when: index !== indexObject.currentIndex 
    } 
} 

main.qml

[...] 
ApplicationWindow { 
    id: root 
    [...] 
    property int currentIndex: 0 

    Screen { 
     index: 0 
     currentIndex: root.currentIndex 
     onTuringActive: root.currentIndex = 0 
    } 
    Screen { 
     index: 1 
     currentIndex: root.currentIndex 
     onTurningActive: root.currentIndex = 1 
    } 
    [...] 
} 

通常情況下,如果只是幾個屬性,您可以創建在您申報實例綁定。如果會有大量的數字,QtObject作爲模型是有益的。
只有當你真的真的確實確定在哪個上下文中將一直使用聲明的組件,那麼你可以考慮引用來自另一個文件的東西(例如,在文件的根節點中使用parent或使用id s需要在另一個文件中解析)

相關問題