2017-06-25 46 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" 
     } 
    } 
}