Qt 使用QML使用fileUrl创建动态图像ListElement
我是QML编程新手。单击文件对话框的“打开”按钮后,我想在Qt 使用QML使用fileUrl创建动态图像ListElement,qt,listview,qml,qtquick2,filedialog,Qt,Listview,Qml,Qtquick2,Filedialog,我是QML编程新手。单击文件对话框的“打开”按钮后,我想在列表模型中创建动态图像作为列表元素s。我的问题是,在添加第二个图像时,第一个图像也会被第二个图像替换。如何单独更新列表元素中的图像?这是我的密码: Component{ id:delegate Item{ height: 100 visible: true width :100 Rectangle{ id: list
列表模型
中创建动态图像作为列表元素
s。我的问题是,在添加第二个图像时,第一个图像也会被第二个图像替换。如何单独更新列表元素中的图像
?这是我的密码:
Component{
id:delegate
Item{
height: 100
visible: true
width :100
Rectangle{
id: list
height: 100
width:height
color : "#20292A"
border.color: "#3A8A86"
border.width: 4
radius: 3
visible:true
Image{
x: 3
y: 3
height : 95
visible: true
width : height
source:mod[index]//fileDialog.fileUrl
}
}
}
}
ListModel{
id:mod
}
Rectangle{
id:listdata
x: 180
y: 577
height: 100
width:841
color : "#20292A"
border.color: "#3A8A86"
border.width: 4
radius: 3
visible:true
Row{
y: 4
height:90
width:841
anchors.fill: list
spacing: 50
visible: true
ListView{
id:view
x: 193
y: 1
width: 841
height: 90
model:mod
clip: true
delegate: delegate
anchors.fill: listdata
anchors.bottomMargin: 78
visible: true
interactive: true
anchors.leftMargin: 190
anchors.left: window.left
anchors.bottom: window.bottom
orientation: Qt.Horizontal
layoutDirection : Qt.LeftToRight
anchors.horizontalCenter: window
anchors.verticalCenter: window
spacing: 50
}
}
}
FileDialog {
id: fileDialog
selectExisting: fileDialogSelectExisting.checked
modality: fileDialogModal.checked ? Qt.WindowModal : Qt.NonModal
title: "Please choose a file"
onAccepted: {
console.log("You chose: " + fileDialog.fileUrls)
mod.append(fileDialog.fileUrls)
}
onRejected: {
console.log("Canceled")
Qt.quit()
}
}
QML ListModel需要一个JSON字典,位于append
而且它不是容器,所以不能访问数组的索引
要使其正常工作,首先需要更改append函数以存储json:
mod.append({"fileUrl": fileDialog.fileUrl.toString()})
然后,您可以通过其JSON名称访问委托调用中的元素:
source: fileUrl
您不必担心委托中的索引位置,它将始终访问当前元素。您可以将代码发布到从FileDialog创建ListElement的位置吗?@beemaster下面是FileDialog代码。我将文件路径附加到Accepted上。@MJKhan Cool,请接受它作为答案:)