[英]QML adding labels dynamically
Allright, i'm trying to make a kind of messenger using qml. 好吧,我正在尝试使用qml制作一种信使。 i have a textarea and a send button.
我有一个textarea和一个发送按钮。 when send button is clicked the text inside the textarea will be displayed somewhere on the screen.
单击发送按钮时,textarea内的文本将显示在屏幕上的某个位置。 but any other changes in text area will change the context of label.
但文本区域中的任何其他更改都将更改标签的上下文。 i tried using createObject(...) but it didn't help.
我尝试使用createObject(...)但它没有帮助。 is there any other way for creating labels (or any other component) dynamically?
有没有其他方式动态创建标签(或任何其他组件)?
import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.3
import QtQuick.Controls.Material 2.1
ApplicationWindow {
visible: true
width: 640
height: 480
property var xPosition : 500
property var yPosition: 200
title: qsTr("server")
Rectangle{
width: parent.width
height: parent.height
Button{
id: sentButton
width: parent.width / 14
x: parent.height + 112
y: parent.width - 200
Material.accent: Material.Blue
Material.background: Material.DeepOrange
Text {
id: name
text: qsTr("Send")
color: "white"
x:parent.width / 4
y:parent.height / 4
}
onClicked: {
//add label with the context of textarea
}
}
Rectangle{
id:back
height: sentButton.height
width: parent.width - sentButton.width
x:0
y: 435
color: "white"
border.width: 0.5
TextArea{
id:search
placeholderText: qsTr("Search")
x:7
width: parent.width - 25
background: null
}
}
}
}
Instead of creating Label
manually, I would add a row to a model (like ListModel
) and display it with a ListView
. 我不是手动创建
Label
,而是向模型添加一行(如ListModel
)并使用ListView
显示它。
The ListView
will instantiate a delegate for each row of the model, it's cleaner than doing it manually. ListView
将为模型的每一行实例化一个委托,它比手动更清晰。 Plus you get the scrolling behaviour for free. 另外,您可以免费获得滚动行为。
Example here : 这里的例子:
import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.3
import QtQuick.Controls.Material 2.1
ApplicationWindow {
visible: true
width: 640
height: 480
Material.accent: Material.DeepOrange
ListModel {
id: messageModel
}
ColumnLayout {
anchors { fill: parent; margins: 8 }
spacing: 16
ListView {
Layout.fillWidth: true; Layout.fillHeight: true
model: messageModel
delegate: ItemDelegate { text: model.message }
}
RowLayout {
spacing: 16
Layout.fillWidth: true; Layout.fillHeight: false
TextField {
id: textField
Layout.fillWidth: true; Layout.fillHeight: true
}
Button {
Material.foreground: "white"; Material.background: Material.DeepOrange
Layout.fillHeight: true
text: "Send"
onClicked: {
messageModel.append({message: textField.text});
textField.text = "";
}
}
}
}
}
Here, the Button
will append a new row to the ListModel
with the TextField
's text
as the message role. 这里,
Button
将向ListModel
追加一个新行, TextField
的text
作为消息角色。 Then the ListView
instantiates for each row of the model a ItemDelegate
displaying the message role. 然后,
ListView
为模型的每一行实例化一个显示消息角色的ItemDelegate
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.