QML中使用Repeater创建差异化按钮:无法添加ListElement图片属性
在QML Repeater按钮中添加图标属性的解决方案
要给ListElement添加图片属性并在按钮中显示,直接按以下步骤操作即可:
- 给ListElement新增
iconSource字段,填入图片路径(支持本地资源路径、Qt内置图标路径) - 给Button的必填属性设置默认值,避免部分ListElement缺省属性时报错
- 利用Button自带的
icon属性绑定模型里的图片路径,还能按需调整图标大小、颜色
修改后的关键代码部分
1. 更新ListModel,添加图标属性
ListModel{ id: fruitModel ListElement { name: "CLIM" colorValue: "gray" iconSource: "image://icons/weather-clear" // Qt内置图标示例,可替换为本地图片路径 } ListElement { name: "Kiwi" colorValue: "sienna" iconSource: "image://icons/emblem-favorite" } ListElement { name: "Banana" colorValue: "gold" iconSource: "image://icons/accessories-text-editor" } }
2. 调整Button代理,支持图标显示
Repeater{ model: fruitModel delegate: Button{ implicitHeight: 90 implicitWidth: 90 onClicked: displayedButton = text id: delegateRoot // 给必填属性设置默认值,兼容缺省字段的ListElement required property string name: "" required property color colorValue: "gray" required property string iconSource: "" text: delegateRoot.name // 绑定图标并设置样式 icon.source: delegateRoot.iconSource icon.width: 40 icon.height: 40 icon.color: "white" // 绑定按钮背景色 background: Rectangle{ color: delegateRoot.colorValue radius: 10 } Layout.alignment: Qt.AlignHCenter } }
完整修改后的Main.qml
//green text for my group members ApplicationWindow{ id: applicationWindow property string displayedButton: "" width: 800 height: 380 visible: true Page { anchors.fill: parent id:page ListModel{ id: fruitModel ListElement { name: "CLIM" colorValue: "gray" iconSource: "image://icons/weather-clear" } ListElement { name: "Kiwi" colorValue: "sienna" iconSource: "image://icons/emblem-favorite" } ListElement { name: "Banana" colorValue: "gold" iconSource: "image://icons/accessories-text-editor" } } GridLayout { id:rec anchors.fill: parent ColumnLayout { anchors.topMargin: 20 y:10 id: leftMenuBar Layout.preferredWidth: 90 Layout.fillHeight: true clip: true spacing: 10 Repeater{ model: fruitModel delegate: Button{ implicitHeight: 90 implicitWidth: 90 onClicked: displayedButton = text id: delegateRoot required property string name: "" required property color colorValue: "gray" required property string iconSource: "" text: delegateRoot.name icon.source: delegateRoot.iconSource icon.width: 40 icon.height: 40 icon.color: "white" background: Rectangle{ color: delegateRoot.colorValue radius: 10 } Layout.alignment: Qt.AlignHCenter } } Layout.alignment: Qt.AlignLeft Layout.leftMargin: 13 } Item { Layout.fillWidth: true Layout.fillHeight: true Rectangle { anchors.fill: parent anchors.margins: 12 border.width: 2 border.color: "lightgray" radius: 35 Text { anchors.centerIn: parent text: "Selected Button: " + displayedButton font.pixelSize: 20 } } } } footer:RowLayout { width: 500 spacing:12 Item { implicitWidth: 3 } Repeater { model: 8 delegate: Button{ implicitHeight: 90 implicitWidth: 90 text: "Button " + (index + 1) onClicked: displayedButton = text Layout.alignment: Qt.AlignHCenter } } } } }
注意事项
- 若使用本地图片,需确保路径正确,比如放在qrc资源文件中时路径格式为
qrc:/xxx.png - Qt内置图标可通过
image://icons/图标名称调用,可参考Qt官方图标列表选择对应名称 - 给required属性设置默认值是为了避免模型中部分元素缺省字段导致运行报错
内容的提问来源于stack exchange,提问作者Jung San
相关产品推荐
相关产品推荐

