Qt QML-为什么TabBar';s隐式宽度取决于绑定内部选项卡按钮';s宽度到它的隐式宽度?

Qt QML-为什么TabBar';s隐式宽度取决于绑定内部选项卡按钮';s宽度到它的隐式宽度?,qt,qml,qt5,qtquick2,qt5.15,Qt,Qml,Qt5,Qtquick2,Qt5.15,为什么在TabBar的以下代码implicitWidth中,当我在TabButtons中注释掉width:implicitWidth时会发生变化?为什么我将组件的宽度设置为等于其隐式宽度会更改隐式宽度? 另外,我还认为当未明确设置width时,width默认设置为implicitWidth。所以我不明白为什么做width:implicitWidth会有任何效果。代码如下: Item { id: root height: 480 width: 640 RowLayout

为什么在
TabBar
的以下代码
implicitWidth
中,当我在
TabButton
s中注释掉
width:implicitWidth
时会发生变化?为什么我将组件的
宽度
设置为等于其
隐式宽度
会更改
隐式宽度
? 另外,我还认为当未明确设置
width
时,
width
默认设置为
implicitWidth
。所以我不明白为什么做
width:implicitWidth
会有任何效果。代码如下:

Item {
   id: root
   height: 480
   width: 640

   RowLayout {
      anchors.fill: parent

      TabBar {
         id: bar
         Layout.fillHeight: true
         implicitWidth: Math.max(homeTabButton.implicitWidth, discoverTabButton.implicitWidth, activityTabButton.implicitWidth)

         background: Rectangle {
            color: "yellow"
         }

         contentItem: ListView {
            model: bar.contentModel
         }


         TabButton {
            id: homeTabButton
            width: implicitWidth // I cannot comment this out
            text: qsTr("Home")
         }
         TabButton {
            id: discoverTabButton
            width: implicitWidth // I cannot comment this out
            text: qsTr("Discover")
         }
         TabButton {
            id: activityTabButton
            width: implicitWidth // I cannot comment this out
            text: qsTr("Activity")
         }
      }

      StackLayout {
         Layout.fillWidth: true
         Layout.fillHeight: true
         currentIndex: bar.currentIndex
         Pane {
            id: homeTab
            background: Rectangle {
               color: "blue"
            }
         }
         Pane {
            id: discoverTab
            background: Rectangle {
               color: "red"
            }
         }
         Pane {
            id: activityTab
            background: Rectangle {
               color: "green"
            }
         }
      }
   }
}

您之前已经指定了一个隐式宽度

implicitWidth: Math.max(homeTabButton.implicitWidth, discoverTabButton.implicitWidth, activityTabButton.implicitWidth)
这就是为什么在tabButton中添加注释宽度时,宽度为0

使用


在您的代码和注释中,请参阅选项卡按钮中的宽度。

您之前已经指定了一个隐式宽度

implicitWidth: Math.max(homeTabButton.implicitWidth, discoverTabButton.implicitWidth, activityTabButton.implicitWidth)
这就是为什么在tabButton中添加注释宽度时,宽度为0

使用


在您的代码和注释中,tabButton中的宽度可参见。

implicitWidth类似于小部件的
sizeHint
宽度。一个暗示可能会被考虑到,也可能不会
Width
是实际宽度。@用户6528273但是为什么将
Width
设置为
implicitWidth
会更改
implicitWidth
?该
implicitWidth
类似于小部件的
sizeHint
宽度。一个暗示可能会被考虑到,也可能不会
Width
是实际宽度。@用户6528273但是为什么将
Width
设置为
implicitWidth
会更改
implicitWidth
?我之前分配的
implicitWidth
是用于
条形图组件的,我为每个选项卡按钮使用的
implicitWidth
是特定按钮的
implicitWidth
。在我看来,您所拥有的并不是一个好的实践,因为建议始终在“儿童尺寸”中参考“家长尺寸”,并在“家长隐式尺寸”中参考“儿童隐式尺寸”,以获得可重用的组件。我之前分配的
隐式宽度
用于
条形
组件,我为每个选项卡按钮使用的
implicitWidth
是特定按钮的
implicitWidth
。在我看来,您所拥有的并不是一个好的实践,因为建议始终在“子级大小”中引用“父级大小”,在“父级隐式大小”中引用“子级隐式大小”,以获得可重用的组件。