QML行元素拒绝工作
我知道我应该使用行、列等,而不是ID锚定的项目,以使代码更简单、更易于阅读。但他们大部分时间都拒绝工作。例如,在这种情况下:QML行元素拒绝工作,qml,qtquick2,Qml,Qtquick2,我知道我应该使用行、列等,而不是ID锚定的项目,以使代码更简单、更易于阅读。但他们大部分时间都拒绝工作。例如,在这种情况下: import QtQuick 2.4 import QtQuick.Controls 1.3 import QtQuick.Layouts 1.1 ListView { id: listView anchors.fill: parent topMargin: spacing anchors.leftMargin: spacing
import QtQuick 2.4
import QtQuick.Controls 1.3
import QtQuick.Layouts 1.1
ListView {
id: listView
anchors.fill: parent
topMargin: spacing
anchors.leftMargin: spacing
anchors.rightMargin: spacing
clip: true
spacing: 0.5 * pxPermm
model: SqlQueryModel {}
delegate: Rectangle {
id: delegateItem
color: "white"
height: 14 * pxPermm
width: listView.width
clip: true
Row {
id: row
anchors.fill: delegateItem
spacing: pxPermm
Image {
height: row.height
width: height
source: "qrc:/resources/ryba.jpg"
fillMode: Image.PreserveAspectCrop
}
Item {
id: textItem
height: row.height
Label {
anchors.left: textItem.left
anchors.top: textItem.top
text: nazov
font.bold: true
}
Label {
anchors.left: textItem.left
anchors.bottom: textItem.bottom
text: cas
}
}
}
}
}
这将在列表视图的代理中的图像顶部显示两个标签。如您所料,图像右侧没有两个标签。但是,该代码在以下情况下起作用:
import QtQuick 2.4
import QtQuick.Controls 1.3
import QtQuick.Layouts 1.1
ListView {
id: listView
anchors.fill: parent
topMargin: spacing
anchors.leftMargin: spacing
anchors.rightMargin: spacing
clip: true
spacing: 0.5 * pxPermm
model: SqlQueryModel {}
delegate: Rectangle {
id: delegateItem
color: "white"
height: 14 * pxPermm
width: listView.width
clip: true
Row {
id: row
anchors.fill: delegateItem
spacing: pxPermm
Image {
height: row.height
width: height
source: "qrc:/resources/ryba.jpg"
fillMode: Image.PreserveAspectCrop
}
Label {
text: nazov
font.bold: true
}
}
}
}
当然,我需要在代理中显示多个标签。我在这里遗漏了什么?事实证明,默认情况下,该项的宽度为零。设置宽度后,代码正常工作:
Item {
id: textItem
height: row.height
width: childrenRect.width
// labels etc
}
您是否尝试更改为行布局/列布局?是的,但是使用RowLayout时,图像被拉伸以填充整个代理:-(但是我发现了问题。