Qt QML-创建可重用的ListView头组件
我在一个页面上有3个列表视图,我想创建一个可用于每个列表的Qt QML-创建可重用的ListView头组件,qt,qml,qt5,qtquick2,qt-quick,Qt,Qml,Qt5,Qtquick2,Qt Quick,我在一个页面上有3个列表视图,我想创建一个可用于每个列表的ListView标题组件 所以我有一个列表视图: ListView { id: listOne spacing: 5 header: headerComponent model: ListOneModel } 它引用以下标题组件: Comp
ListView
标题组件
所以我有一个列表视图:
ListView {
id: listOne
spacing: 5
header: headerComponent
model: ListOneModel
}
它引用以下标题组件:
Component {
id: headerComponent
Rectangle {
width: ListView.view.width
height: 50
Label {
text: "List One"
font.pixelSize: 20
elide: Label.ElideRight
width: ListView.view.width
padding: {
left: 14
}
background: Rectangle {
color: "red"
}
}
}
}
如何使标题组件可重用,以便在将ListView
连接到标题时,也可以传递不同的标题
我知道我可以更改标题组件并添加titleText
属性,如下所示:
Component {
id: headerComponent
property string titleText: "My Title"
Rectangle {
width: ListView.view.width
height: 50
Label {
text: titleText
font.pixelSize: 20
elide: Label.ElideRight
width: ListView.view.width
padding: {
left: 14
}
background: Rectangle {
color: "red"
}
}
}
}
但是在我的ListView中使用header组件时,如何指定titleText属性?创建新文件调用
ListHeader。qml
包含您的头:
import QtQuick 2.0
import QtQuick.Controls 1.1
Rectangle {
property alias name: mylabel.text
width: ListView.view.width
height: 50
Label {
id: mylabel
text: "List One"
font.pixelSize: 20
elide: Label.ElideRight
width: parent.width
padding: {
left: 14
}
background: Rectangle {
color: "red"
}
}
}
然后像这样使用它:
ListView {
header: ListHeader{
name: "ListOneNewName"
}
}
QML文档关于和。创建新文件调用
ListHeader。QML
包含您的标题:
import QtQuick 2.0
import QtQuick.Controls 1.1
Rectangle {
property alias name: mylabel.text
width: ListView.view.width
height: 50
Label {
id: mylabel
text: "List One"
font.pixelSize: 20
elide: Label.ElideRight
width: parent.width
padding: {
left: 14
}
background: Rectangle {
color: "red"
}
}
}
然后像这样使用它:
ListView {
header: ListHeader{
name: "ListOneNewName"
}
}
QML文档关于和。您可以在每个listview中设置一个属性,然后从Header组件中访问该属性 例如:-
ListView {
id: listOne
property string headertitle: "list one header"
spacing: 5
header: headerComponent
model: ListOneModel
}
ListView {
id: listTwo
property string headertitle: "list two header"
spacing: 5
header: headerComponent
model: ListTwoModel
}
ListView {
id: listThree
property string headertitle: "list three header"
spacing: 5
header: headerComponent
model: ListThreeModel
}
Component {
id: headerComponent
Rectangle {
width: ListView.view.width
height: 50
Label {
text: ListView.view.headertitle
font.pixelSize: 20
elide: Label.ElideRight
width: ListView.view.width
padding: {
left: 14
}
background: Rectangle {
color: "red"
}
}
}
}
您可以在每个listview中设置一个属性,然后从Header组件中访问该属性 例如:-
ListView {
id: listOne
property string headertitle: "list one header"
spacing: 5
header: headerComponent
model: ListOneModel
}
ListView {
id: listTwo
property string headertitle: "list two header"
spacing: 5
header: headerComponent
model: ListTwoModel
}
ListView {
id: listThree
property string headertitle: "list three header"
spacing: 5
header: headerComponent
model: ListThreeModel
}
Component {
id: headerComponent
Rectangle {
width: ListView.view.width
height: 50
Label {
text: ListView.view.headertitle
font.pixelSize: 20
elide: Label.ElideRight
width: ListView.view.width
padding: {
left: 14
}
background: Rectangle {
color: "red"
}
}
}
}
是否可以将ListHeader.qml放在子目录(可能是命名组件)中,并以这种方式引用它@是的,对于文件夹
components
,只需导入“components”,是否可以将ListHeader.qml放在子目录(可能是命名组件)中,并以这种方式引用它@是的,对于文件夹组件
,只需导入“组件”