2016-04-13 38 views
1

我在我的主要QML文件中有一個Loader對象。我在運行時根據當前上下文在加載器中加載不同的QML源代碼。如何讓裝載程序不再填充其父項?

我的步驟是這樣的:

  1. 加載一個login.qml文件和裝載程序設置anchors.centerIn: parent
  2. 成功登錄後,我加載task.qml,然後在加載器上設置anchors.fill: parent
  3. 用戶註銷後,我想重定向回login.qml,並在加載器上再次設置anchors.centerIn: parent

我預計這會導致加載程序在父級中居中,不再填充它。然而,這種情況並非如此。裝載程序仍填充父項。

如何更改項目上的錨點,使其再次居中並不再填充其父項?

回答

1

您不需要再次設置anchors.centerIn。相反,您需要將anchors.fillwidthheight設置爲undefined,因此它默認爲隱式大小並再次使用anchors.centerIn屬性。

這裏有一個工作示例:

import QtQuick 2.0 
import QtQuick.Controls 1.0 

Item { 
    width: 800 
    height: 600 

    Rectangle { 
     id: rect 
     anchors.centerIn: parent 

     implicitWidth: 500 
     implicitHeight: 200 
     color: "red" 

     Button { 
      text: "Toggle Full Size" 
      anchors.centerIn: parent 
      onClicked: { 
       if (rect.anchors.fill) { 
        rect.anchors.fill = undefined 
        rect.width = undefined 
        rect.height = undefined 
       } else { 
        rect.anchors.fill = rect.parent 
       } 
      } 
     } 
    } 
} 

另外,一個更簡單的解決辦法可能是讓你的裝載機始終填補其母公司,而是在你login.qml文件的頂層添加一個額外的項目,以便登錄的觀點是以此項目爲中心。這將消除改變Loader上錨點的必要性。