我尝试将ListView包装在一个Rectangle中并且它成功但现在我遇到了问题。标题和委托行显示在同一行,如下所示:https://imgur.com/a/IAQm9Y6
我希望它像这样,但它不可滚动:https://imgur.com/2f1bIDw
这是我的代码:
Rectangle {
id:listViewRec
visible: true
anchors.fill: parent
anchors.topMargin: 100 * wdw.height / 1080
anchors.bottomMargin: 30 * wdw.height / 1080
anchors.leftMargin: 30 * wdw.width / 1920
anchors.rightMargin: 30 * wdw.width / 1920
ListView {
id: listViewDispInf
anchors.fill: parent
width: listViewRec.width
height: listViewRec.height
clip: true
orientation: ListView.Horizontal
flickableDirection: Flickable.HorizontalFlick
boundsBehavior: Flickable.StopAtBounds
model: batteries
header: Row {
function itemAt(index) { return repeater1.itemAt((index)) }
Repeater {
id: repeater1
model: Parameters { }
Rectangle {
height: 50 * wdw.height / 1080
width: 1.1 * txt1.paintedWidth
Text {
id: txt1
anchors.centerIn: parent
text: name
}
}
}
}
delegate : Row {
id: delegate
property var informations: model.modelData
function itemAt(index) { return repeater2.itemAt((index)) }
Repeater {
id: repeater2
model: 26
Rectangle {
height: (listViewRec.height - listViewDispInf.headerItem.itemAt(index).height ) / 6
width: listViewDispInf.headerItem.itemAt(index).width
Text {
anchors.centerIn: parent
text: delegate.informations.getInformations(index)
}
}
}
}
}
}
该模型来自我的main.cpp,如下所示:
QList<QObject *> dataList;
dataList.append(b1);
engine.rootContext()->setContextProperty("batteries", QVariant::fromValue(dataList));
答案 0 :(得分:0)
您没有为ListView
orientation
使用正确的值。
您应该删除该行或使用orientation: ListView.Vertical
编辑:另外,查看您的代码,看起来您实际上正在寻找TableView
。
您可以使用不同的列和resizeColumnsToContents方法,而不是使用行并计算每个项目的宽度。