2017-09-02 80 views
0

我有2 QQuickItem s如下所示,我可以使用QMLEngine這樣在C++端獲取。如何將QQuickItem的位置轉換爲其新父項

QQuickItem * quick_item_1 = m_qml_engine->rootObjects()[0]->findChild<QQuickItem*>("quickitem1"); 
QQuickItem * quick_item_2 = m_qml_engine->rootObjects()[0]->findChild<QQuickItem*>("quickitem2"); 

注:quick_item_1的母公司爲不同 & quick_item_2的直接父也不同。 但是他們都在同一個應用程序窗口繪製到不同的直接父母。

我正在畫他們兩個脫屏QQuickItem。我們稱之爲new_parent_surface。我通過將new_parent_surface更改爲new_parent_surface這樣來繪製這兩個項目。

quick_item_1->setParentItem(new_parent_surface); 
quick_item_2->setParentItem(new_parent_surface); 

這工作正常繪製他們在新的父QQuickItem的目標。我在new_parent_surface上得到quick_item_1 & quick_item_2。即使new_parent_surface未在UI上繪製,但如果使用grabToImage的拍攝快照,我可以看到在它們上繪製的2個項目。細到這裏。

然而定位quick_item_1 & quick_item_2是不正確的。我想將它們放置成與他們放置原始父項目的方式類似。我可以做一些百分比數學運算&嘗試將它們放置在原始父級上,但是不存在用於將此定位轉換爲新父級的QQQuickItem或Qt API?

我試圖查看QQuickItem的映射API,如mapToItem &這樣嘗試它們。

quick_item_2->mapToItem(new_parent_surface, quick_item_2->position()); 

但是定位依然不正確。

那麼,如何我可以在做一個setParentItem後將QQuickItem的位置映射到它的新父母QQuickItem?

+0

'QQuickItem'有很多'mapTo *()'和'manFrom *()'函數。順便說一句,下一次只是把你的代碼放在這裏,而不是解釋它。 – folibis

+0

瞭解@folibis。由於代碼中出現其他事情,我認爲描述相關問題會更好。順便說一句,如果我嘗試帶有一些值的'setPosition',那麼當然我可以像以前一樣定位'quick_item_2'。但我有興趣知道是否有推薦的QQuickItem API來執行此操作 –

回答

1

Item s位置總是相對於其父母。父母的位置相對於其父母等等。但你總是可以獲得相對或全球的位置。 QML有很多協調翻譯功能。這裏是一個小例子,可以解釋這個問題:

import QtQuick 2.7 
import QtQuick.Window 2.0 
import QtQuick.Controls 2.1 

Window { 
    id:container 
    width: 800 
    height: 800 
    visible: true 

    Component { 
     id: rect 
     Rectangle { 
      property bool imParent: false 
      x: 50 + Math.round(Math.random() * 550) 
      y: 50 + Math.round(Math.random() * 550) 
      width: 100 + Math.round(Math.random() * 100) 
      height: 100 + Math.round(Math.random() * 100) 
      color: Qt.rgba(Math.random(),Math.random(),Math.random(),1); 
      Drag.active: dragArea.drag.active 
      MouseArea { 
       id: dragArea 
       anchors.fill: parent 
       drag.target: parent 
      } 
      Text { 
       anchors.centerIn: parent 
       text: imParent ? "I'm parent" : "Drag me" 
       color: "white" 
      } 
     } 
    } 

    Rectangle { 
     id: blk 
     x: 10 
     y: 10 
     z: 100 
     parent: null 
     height: 50 
     width: 50 
     radius: 5 
     border.color: "white" 
     color: "black" 

    } 

    Repeater { 
     id: reptr 
     model: 5 
     property int pos: 0 
     Loader { 
      id: loader 
      sourceComponent: rect 
      onLoaded: { 
       if(blk.parent == null) { 
        blk.parent = loader.item; 
        loader.item.imParent = true; 
       } 
      } 
     } 
    } 

    Row { 
     anchors.horizontalCenter: container.contentItem.horizontalCenter 
     spacing: 2 
     Button { 
      text: "Reparent relative to parent" 
      onClicked: { 
       reptr.pos ++; 
       if(reptr.pos >= reptr.model) { 
        reptr.pos = 0; 
       } 
       var item = reptr.itemAt(reptr.pos).item; 
       blk.parent.imParent = false; 
       blk.parent = item; 
       blk.parent.imParent = true; 
      } 
     } 
     Button { 
      text: "Reparent relative to scene" 
      onClicked: { 
       reptr.pos ++; 
       if(reptr.pos >= reptr.model) { 
        reptr.pos = 0; 
       } 
       var item = reptr.itemAt(reptr.pos).item; 
       var coord = blk.mapToGlobal(blk.x, blk.y); 
       blk.parent.imParent = false; 
       blk.parent = item; 
       blk.parent.imParent = true; 
       coord = blk.mapFromGlobal(coord.x, coord.y); 
       blk.x = coord.x; 
       blk.y = coord.y; 
      } 
     } 
    } 
}