扩展TabViewStyle styleData [英] Extending TabViewStyle styleData
问题描述
我目前正在尝试找到一种更好的方法,将一个图标添加到选项卡.现在,我很乐意退出styleData.title以包含图标源,但是能够扩展styleData以便我可以包含其他自定义属性会很好.
I am currently trying to find a nicer way to do this, add an icon to a tab. Right now I am piggy backing off the styleData.title to include the icon source, but it would be nice to be able to extend the styleData so I can include other custom properties.
这是我当前的骇客:
Tab {
...
title: "Home|images/home-75.png"
...
}
style: TabViewStyle {
...
tab: Rectangle {
...
Text {
...
text: styleData.title.split("|")[0]
...
}
Image {
...
source: styleData.title.split("|")[1]
}
}
}
但是,这样做会更好:
Tab {
...
title: "Home"
source: "images/home-75.png"
...
}
style: TabViewStyle {
...
tab: Rectangle {
...
Text {
...
text: styleData.title
...
}
Image {
...
source: styleData.source
}
}
}
以下是应用程序标记:
import QtQuick 2.2
import QtQuick.Window 2.1
import QtQuick.Layouts 1.1
import QtQuick.Controls 1.1
import QtQuick.Controls.Styles 1.1
ApplicationWindow {
visible: true
width: 320
height: 480
TabView {
id: tabwidget
x: 0
y: 0
tabPosition: Qt.BottomEdge
width: parent.width
height: parent.height
Tab {
id: homeTab
title: "Home|images/home-75.png"
source: "images/home-75.png"
component: Qt.createComponent("Page2.qml")
}
Tab {
id: inboxTab
title: "Inbox|images/home-75.png"
component: Qt.createComponent("Page3.qml")
}
Tab {
id: outboxTab
title: "Outbox"
source: "images/home-75.png"
component: Qt.createComponent("Page3.qml")
}
Tab {
id: settingTab
title: "Settings"
source: "images/home-75.png"
component: Qt.createComponent("Page3.qml")
}
style: TabViewStyle {
frameOverlap: 0
tab: Rectangle {
color: "#F6F6F7"
border.width: 0
implicitWidth: (parent.control.width + 3) / 4
implicitHeight: 88
radius: 0
CustomBorder
{
commonBorder: false
lBorderwidth: 0
rBorderwidth: 0
tBorderwidth: 1
bBorderwidth: 0
borderColor: "#bababc"
}
Text {
id: text
anchors.bottom: parent.bottom
anchors.horizontalCenter: parent.horizontalCenter
text: styleData.title.split("|")[0]
color: styleData.selected ? "#ff3b30" : "#8e8e8f"
}
Image {
id: img
width: 75
height: 75
source: styleData.title.split("|")[1]
}
}
}
}
}
推荐答案
创建组件IconTab.qml
import QtQuick 2.2
import QtQuick.Controls 1.1
Tab {
property url iconSource
}
然后替换所有Tab
宽度IconTab
s,例如
Then replace all your Tab
s width IconTab
s, e.g.
IconTab {
id: outboxTab
title: "Outbox"
iconSource: "images/home-75.png"
}
并通过以下方式在TabViewStyle中获取图标的来源:
And get the icon's source in the TabViewStyle via:
Component.onCompleted: {
console.log("title: " + styleData.title)
console.log("title: " + tabwidget.getTab(styleData.index).title)
console.log("icon: " + tabwidget.getTab(styleData.index).iconSource)
}
顺便说一句.请查看 Tab.source 和 Tab.sourceComponent . Tab.component应该不起作用.至少没有记录.
Btw. check out the documentation for Tab.source and Tab.sourceComponent. Tab.component should not work. At least, it is not documented.
这篇关于扩展TabViewStyle styleData的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!