QML中动态表格修改数据

1.qml文件中的实现代码

javascript 复制代码
import QtQuick 2.15
import QtQuick.Window 2.15

import QtQuick.Controls 2.0
import Qt.labs.qmlmodels 1.0
import QtQuick.Layouts 1.15

Window {
    width: 640
    height: 480
    visible: true
    title: qsTr("Hello World")


    TableModel
    {
        id:table_model
        TableModelColumn { display: "name" }
        TableModelColumn { display: "item1" }
        TableModelColumn { display: "item2" }
        TableModelColumn { display: "item3" }
        TableModelColumn { display: "item4" }

    }


    Column
    {

        TableView
        {
            id:tabel_view_statistics

            visible: true

            implicitHeight: 100
            implicitWidth: 300

            anchors.horizontalCenter: parent.horizontalCenter


            columnSpacing: -1
            rowSpacing: -1
            clip: true

            delegate: Rectangle
            {
                implicitWidth: 50
                implicitHeight: 25
                border.width: 1

                Text
                {
                    text: display
                    anchors.centerIn: parent
                }
            }
        }

        Label{

            id:label_01
        }

        Button{

            id:button1
            text: "动态数据"

            onClicked: {


                table_model.appendRow(                        {
                                         "name": "树木",
                                         "item1": "6",
                                         "item2": "",
                                         "item3": "",
                                         "item4": ""
                                         })

                tabel_view_statistics.model = table_model


            }
        }

        Button{

            id:button2
            text: "修改数据"

            onClicked: {

                table_model.setRow(0,{
                                       "name": "森林",
                                       "item1": "9",
                                       "item2": "",
                                       "item3": "",
                                       "item4": ""
                                       })

                tabel_view_statistics.model = table_model

           


            }

        }


    }

}

2.效果图如下:

相关推荐
Rookie Linux3 天前
使用Qt6 QML以及第三方库FluentUI、PCapPlusPlus开发一个自定义抓包软件
网络·c++·qt·cmake·qml
谁刺我心5 天前
[QtCPP]Examples使用示例-QtMultimedia、QMediaPlayer、Audio音频引擎测试mp3播放
qt·音视频·qml
Quz5 天前
Qt Quick 粒子系统(二):系统控制与生命周期管理
qt·qml·粒子系统
Quz5 天前
Qt Quick 粒子系统(三):发射器深度解析
qt·qml·粒子系统
nnnnichijou6 天前
Qt 6.9 嵌入式 Linux 交叉编译全栈填坑指南(以树莓派5 AArch64 为例
qt·嵌入式·交叉编译·qml·树莓派5
Quz8 天前
Qt Quick 粒子系统(一):架构总览与四层模型
qt·架构·qml
Ulyanov14 天前
深入QML-Python通信 构建响应式交互界面的桥梁设计:QML+PySide6现代开发入门(五)
开发语言·python·算法·交互·qml·系统仿真
东方.既白23 天前
QML简易地铁导乘屏
qml
Ulyanov1 个月前
PySide6 + QML 混合编程全景解析:从底层原理到企业级实战
python·pyside6·qml·雷达电子对抗
_君莫笑1 个月前
Qt+Qml前后端分离上位机软件技术方案
c++·qt·用户界面·qml