我在组件目录中有一个Table.qml文件,其内容为:
import QtQuick 2.12
import QtQuick.Controls 2.12
import QtQuick.Layouts 1.12
TableView {
anchors.fill: parent
columnSpacing: 1
rowSpacing: 1
clip: true
delegate: Rectangle {
implicitWidth: 180
implicitHeight: 70
border.color: "black"
color: (heading==true)?"#dddddd":"white"
Text {
text: tabledata
anchors.centerIn: parent
}
}
}
这在另一个QML文件中使用,例如:
import QtQuick 2.0
import QtQuick.Controls 2.12
import QtQuick.Layouts 1.12
import "../../components" as Components
Rectangle {
id: storeroomScanIncoming
objectName: "ScanIncoming"
color: "white"
property string pageTitle: "Add Incoming Stock"
property string backText: "Storeroom home"
ColumnLayout {
id: home
anchors.leftMargin: parent.width * 0.1
anchors.rightMargin: parent.width * 0.1
anchors.fill: parent
anchors.horizontalCenter: parent.horizontalCenter
Components.PageTitle {
title: pageTitle
}
Text {
text: "Detected Items"
font.pointSize: 10
color: "#B70020"
horizontalAlignment: Text.AlignLeft
Layout.fillWidth: true
}
Rectangle {
color: "transparent"
Layout.fillWidth: true
Layout.preferredHeight: parent.height * 0.6
border.color: "#504338"
border.width: 1
Components.Table {
model: _scannedItemTableModelAPI
}
}
RowLayout {
Item {
//spacer Item {
Layout.fillWidth: true
Rectangle {
anchors.fill: parent
color: "transparent"
}
}
Components.TransparentButton {
text: "Confirm"
onClicked: {
stackView.push("ScanIncomingSummary.qml")
}
}
Components.TransparentButton {
text: "Rescan"
onClicked: {
ScanManager.startScan()
timer.start()
}
}
}
Item {
//spacer Item {
Layout.fillWidth: true
Layout.fillHeight: true
Rectangle {
anchors.fill: parent
color: "white"
}
}
}
}
使用的_scannedItemTableModelAPI模型包含一个QtStringList。它只有1列数据。但是,当我运行该应用程序时,单列并没有占据TableView所在的Rectangle的整个宽度。