【发布时间】:2020-09-24 03:42:08
【问题描述】:
我正在 Qt 5.15 中创建自定义 QML Quick 2 TabBar 控件。如果我将一个简单的控件(CustomTabBarSimple.qml)设为
import QtQuick 2.0
import QtQuick.Controls 2.15
TabBar {
}
然后我可以使用它
import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.15
Window {
visible: true
width: 640; height: 480
color:"silver"
CustomTabBarSimple
{
id:bar
TabButton { text:"Button1" }
TabButton { text:"Button2" }
}
StackLayout {
anchors.top: bar.bottom
currentIndex: bar.currentIndex
Text {text: "First Stack"}
Text {text: "Second Stack"}
}
}
但是,如果我将 TabBar 包装在另一个控件中,这样就不再起作用了:
import QtQuick 2.0
import QtQuick.Controls 2.15
Rectangle
{
default property alias contents: bar.data
width: 300; height:50
color: "red"
TabBar {
id: bar
}
}
您可以看到我尝试使用default property alias contents: bar.data 将TabButton 放在自定义控件的TabBar 中,但似乎TabBar 不再正确组织按钮并且它们不再更改currentIndex 时点击——可能是因为 data 字段正在覆盖关键元素。
是否可以使用默认别名将TabButton 注入正确的位置?其次,我怎样才能从文档中自己发现这一点?
【问题讨论】:
-
如果在
id: bar之后添加anchors.fill: parent会发生什么?