qml 动态创建TableView

在qml应用开发中,经常会用到TableView来显示数据,而有时表格又不是固定的,需要根据不同的情况来变换表格的标题和内容。

本次博客将会介绍怎样在qml中动态的创建TableView。

首先创建一个文件DataShowTable.qml,首字母要大写,这些大家应该都知道吧。

import QtQuick 2.7
import QtQuick.Controls 1.4

TableView{
    id: tableview
    property var holdMenuModelDataArray: null
    backgroundVisible: false;
    frameVisible: false;
    itemDelegate: StandardTabelItemDelegate{} //添加自己喜欢的代理,这个就不用说了吧
    headerDelegate: headerDele;  //表头委托 
    rowDelegate: rowDele;   //行委托 
    model: ListModel{}
    //定义表头的委托
    Component{
        id:headerDele
        Rectangle{
            id: headerRect;
            height: tableview.headerHeight;
            border.color: frameBorderColor;
            color:"transparent";
            radius: 3;
            Text{
                text: styleData.value
                anchors.centerIn: parent
                font.family: "微软雅黑";
                font.pixelSize: 12;
                color: "white";
            }
        }
    }

    //定义行的委托
    Component{
        id:rowDele
        Rectangle{
            id: rect;
            height: 42;
            color: "transparent";
        }
    }
    //更新表格标题列高度和role
    function updateColumn2Table(arrayData){
        if(arrayData.length != 3){
            console.log("DataShowTable.qml updateColumn2Table arrayData.length !=3");
            return;
        }

        var arrayTitle = arrayData[0]; //标题数组 
        var arrayWidth = arrayData[1]; //宽度数组 
        var arrayRoles = arrayData[2]; //role数组 
        if(arrayTitle.length != arrayWidth.length || arrayTitle.length > arrayRoles.length){
            console.log("arrayTitle argument error in DataShowTable.qml updateColumn2Table(arrayTitle,arrayWidth,arrayRoles)");
            return;
        }

        tableview.model.clear(); //先将model里面的数据清空避免在更新的列里面有相同的role直接显示
        var i,str,count = tableview.columnCount;
        var sum = new Number(0);
        //求出长度累加和以便于按比例分配列宽  
        for(i =0;i<arrayWidth.length;i++){
            sum += arrayWidth[i];
        }
        var prefix = 'import QtQuick 2.7;import QtQuick.Controls 1.4;TableViewColumn {width: Math.round(tableview.width *'; //创建TableViewColumn的代码
       //前缀
        //将现在的每列全部删除
        for(i=0;i<count;i++){
            tableview.removeColumn(0);
        }
        //循环添加TableViewColumn
        for(i=0;i<arrayTitle.length;i++){
            str = prefix + arrayWidth[i]/sum +");role:\"" + arrayRoles[i] + "\";title:\"" + arrayTitle[i] + "\"}";
            tableview.addColumn(Qt.createQmlObject(str,tableview,"dynamicSnippet1"));
        }
    }
}

这里还是把itemDelegate的代码也贴出来,让大家清楚全过程。
import QtQuick 2.7
import QtQuick.Controls 1.4

Rectangle{
    id: tableCell
    anchors.fill: parent;
    anchors.margins: 3;
    border.color: "blue";
    radius:3;
    color: styleData.selected ? "transparent" : "#1A4275";

    Text{
        id: textID;
        text:styleData.value ;
        font.family: "微软雅黑";
        font.pixelSize: 12;
        anchors.fill: parent;
        color: "white";
        elide: Text.ElideRight;
        verticalAlignment: Text.AlignVCenter
        horizontalAlignment: Text.AlignHCenter
    }
}
委托也很简单,一个选择变化的矩形和一个内容显示,这样就可以动态的使用了。

下面是主界面中的调用
Item {
    id: root
    property var bankPaySearchTableArray: [["银行卡号","开户行","卡种类型","归属地","止付开始时间","止付结束时间","状态"],
                                           [155,125,110,125,135,135,97],
                                           ["card","bank","type","location","opeStartDate","opeEndDate","cardStatus"]]
    DataShowTable{
        id: table;
        anchors.fill: parent
        anchors.centerIn: parent
        anchors.margins: 18
        Component.onCompleted: {
            Compone table.updateColumn2Table(bankPaySearchTableArray);    //创建表
        }
    }

重点在于要利用TableView提供的函数addColumn()来添加,而创建的话则使用createQmlObject()。以下为效果图,表中数据未在上面

代码给出


  • 10
    点赞
  • 54
    收藏
    觉得还不错? 一键收藏
  • 9
    评论
QML 中的 TableView 是一个用于表格式呈现数据的组件。它可以类比于 Excel 中的表格效果。在给定的 QML 文件中的 TableView 示例中,通过定义 TableViewColumn 元素来描述表格的每一列。这些 TableViewColumn 元素具有不同的属性,如 role(角色)、title(标题)和width(宽度)。在 TableView 中,可以使用 ListModel 来作为数据模型,通过 ListElement 定义每一行的数据。通过将 ListModel 绑定到 TableView 的 model 属性,可以将数据显示在 TableView 中。在给定的示例中,TableView 有三列,分别是 name、cost 和 manufacture。 除了在 QML 文件中直接定义表格列外,也可以使用 Component.onCompleted 来动态地向 TableView 中添加列。通过调用 Qt.createQmlObject 函数创建一个新的 TableViewColumn 对象,然后将其添加到 TableView 中。在给定的示例中,通过创建一个新的 TableViewColumn 对象来添加名为 mamifacturer 的列。 因此,QML 中的 TableView 组件可以通过定义 TableViewColumn 元素来创建表格,并使用 ListModel 作为数据模型,将数据显示在表格中。它提供了一种方便的方式来展示和操作表格式的数据。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [QML TableView 使用详解](https://blog.csdn.net/u013873761/article/details/125064741)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值