如何解决内置TabBar的自定义QML Quick 2控件
我正在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
注入适当的位置?其次,我如何才能从文档中自己发现这一点?
解决方法
您的代码有两个问题:
- 您正在尝试从TabBar访问
currentIndex
,但没有在CustomTabBarSimple中公开该属性。您可以通过添加以下内容来解决此问题:
property alias currentIndex: bar.currentIndex
- TabButton的列表不是TabBar的直接子代。 TabBar派生自容器,该容器在
contentData
中具有子对象列表。您可以阅读有关here的信息。
default property alias contents: bar.contentData
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。