QML 在多行文本流中使用最后一行的剩余宽度
QML use remaining width of last line in a Flow with multi-line Text
在 QML 中,我想将一些文本放入有限的 space 中。此文本包含本地化的、静态的和可变的部分(文件名)。可变部分可能太长,无法放入我拥有的 space 中,如果发生这种情况,应该将其省略。第一部分允许换行,是否本地化可能如此。
我现在的问题如下:两个文本都在 Flow 容器中,目的是将文件名附加到静态文本。但是,如果第一个文本部分换行,整个文本具有我可用的最大宽度并且文件名将放在新行中,即使第一部分的最后一行没有完全填满 space .看这张图片:
代码:
Flow {
width: parent.width
spacing: 4
Text {
width: (contentWidth <= parent.width) ? contentWidth : parent.width
text: qsTr("A string that might or might not wrap, depending on localisation")
wrapMode: Text.WordWrap
}
Text {
width: (contentWidth <= parent.width) ? contentWidth : parent.width
text: fileName
color: customColor
elide: Text.ElideMiddle
// ... more options
MouseArea {
anchors.fill: parent
onClicked: //stuff
}
}
}
是否可以使用最后一行剩余的space?
编辑:它应该是这样的:
我想出的唯一方法很老套。
问题是,Text
是一个对象,周围有一个矩形边界框。 Flow
使用此边界框进行定位。
我的 hacky 解决方案背后的想法是,将文本分解为单个单词,然后由 Flow
定位。现在 Flow
知道最后一个词在哪里结束,因此能够将下一个 Text
放在它后面。
Flow {
id: flw
property string text: "Lorem ipsum dolor sit amet, consectetur adipisici elit, sed eiusmod tempor incidunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquid ex ea commodi consequat. Quis aute iure reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint obcaecat cupiditat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
width: 300
spacing: fnt.advanceWidth(' ')
FontMetrics {
id: fnt
}
Repeater {
id: rep
model: flw.text.split(' ')
delegate: Text {
font: fnt.font
text: modelData
}
}
Text {
text: "C:/Some/Path/To/The/File/In/Question/File.extension"
color: 'green'
width: {
var neighbor = rep.itemAt(rep.count - 1)
parent.width - neighbor.x - neighbor.width - parent.spacing
}
elide: Text.ElideMiddle
}
}
在 QML 中,我想将一些文本放入有限的 space 中。此文本包含本地化的、静态的和可变的部分(文件名)。可变部分可能太长,无法放入我拥有的 space 中,如果发生这种情况,应该将其省略。第一部分允许换行,是否本地化可能如此。
我现在的问题如下:两个文本都在 Flow 容器中,目的是将文件名附加到静态文本。但是,如果第一个文本部分换行,整个文本具有我可用的最大宽度并且文件名将放在新行中,即使第一部分的最后一行没有完全填满 space .看这张图片:
代码:
Flow {
width: parent.width
spacing: 4
Text {
width: (contentWidth <= parent.width) ? contentWidth : parent.width
text: qsTr("A string that might or might not wrap, depending on localisation")
wrapMode: Text.WordWrap
}
Text {
width: (contentWidth <= parent.width) ? contentWidth : parent.width
text: fileName
color: customColor
elide: Text.ElideMiddle
// ... more options
MouseArea {
anchors.fill: parent
onClicked: //stuff
}
}
}
是否可以使用最后一行剩余的space?
编辑:它应该是这样的:
我想出的唯一方法很老套。
问题是,Text
是一个对象,周围有一个矩形边界框。 Flow
使用此边界框进行定位。
我的 hacky 解决方案背后的想法是,将文本分解为单个单词,然后由 Flow
定位。现在 Flow
知道最后一个词在哪里结束,因此能够将下一个 Text
放在它后面。
Flow {
id: flw
property string text: "Lorem ipsum dolor sit amet, consectetur adipisici elit, sed eiusmod tempor incidunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquid ex ea commodi consequat. Quis aute iure reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint obcaecat cupiditat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
width: 300
spacing: fnt.advanceWidth(' ')
FontMetrics {
id: fnt
}
Repeater {
id: rep
model: flw.text.split(' ')
delegate: Text {
font: fnt.font
text: modelData
}
}
Text {
text: "C:/Some/Path/To/The/File/In/Question/File.extension"
color: 'green'
width: {
var neighbor = rep.itemAt(rep.count - 1)
parent.width - neighbor.x - neighbor.width - parent.spacing
}
elide: Text.ElideMiddle
}
}