2017-06-25 43 views
0

在Qt 5.9中的QML應用程序中,我想在Flow中顯示Rectangle s,其固定寬度爲s。爲了保持佈局居中,我嘗試動態調整其填充。要計算正確的值,雖然我需要知道當前的列數,例如colCount。有沒有辦法得到它?QML流中的列數

這裏是我想使用僞變量colCount做一個代碼示例:

import QtQuick 2.7 
import QtQuick.Controls 2.2 
import QtQuick.Layouts 1.3 

ApplicationWindow { 
    visible: true 
    width: 800 
    height: 600 
    title: qsTr("Example") 

    Flow { 
     anchors.fill: parent 
     leftPadding: parent.width > 200*colCount ? 0.5*(parent.width - 200*colCount) : 0 
     spacing: 0 

     Rectangle { 
      width: 200 
      height: 200 
      color: "red" 
     } 

     Rectangle { 
      width: 200 
      height: 200 
      color: "blue" 
     } 
    } 
} 

回答

0

張貼:)我管理後不久的魔力去弄清楚,我發佈這裏的解決方案適用於可能需要它的人。

有一個財產childrenRect.width繼承自項目可以派上用場。所以列數colCount可以計算爲childrenRect.width/w,其中w是孩子的寬度(對於這個人來說,它應該與FLOW的所有孩子相同)。

在從問題的代碼例如:

import QtQuick 2.7 
import QtQuick.Controls 2.2 
import QtQuick.Layouts 1.3 

ApplicationWindow { 
    visible: true 
    width: 800 
    height: 600 
    title: qsTr("Example") 

    Flow { 
     anchors.fill: parent 
     leftPadding: parent.width > childrenRect.width ? 0.5*(parent.width - childrenRect.width) : 0 
     spacing: 0 

     Rectangle { 
      width: 200 
      height: 200 
      color: "red" 
     } 

     Rectangle { 
      width: 200 
      height: 200 
      color: "blue" 
     } 
    } 
}