Qt Quick之TableView的使用

 本博只是简单的展示TableView的基本使用(TableView、style:TableViewStyle、headerDelegate、rowDelegate、itemDelegate、TableViewColumn、ListModel及访问和修改Model),关于更多属性和方法的使用可以参考TableView QML Type

  

2. 代码实现

import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Controls 1.4
import QtQuick.Controls.Styles 1.4

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

    TableView
    {
        id:stockTable;

        anchors.left: parent.left;
        anchors.top:parent.top;
        anchors.topMargin: 10;
        anchors.right: parent.right;
        anchors.bottom: parent.bottom;
        //alternatingRowColors : true;
        style:TableViewStyle
        {
            id:tstyle;
            backgroundColor:"white";
            alternateBackgroundColor:"#f6F6F6";
            textColor:"black";

            // 设置TableView的头部
            headerDelegate: Canvas
            {
                implicitWidth:100;
                implicitHeight:32;

                onPaint:
                {
                    var ctx = getContext("2d");
                    ctx.lineWidth = 2;
                    ctx.strokeStyle="red";
                    ctx.fillStyle="blue";

                    ctx.beginPath();
                    console.log("width:",width,"--","height:",height);
                    ctx.rect(0,0,width,height);
                    ctx.stroke();
                    ctx.font="14pt sans-serif";
                    ctx.textAlign="right"
                    ctx.textBaseLine="middle";
                    ctx.fillText(styleData.value,width-2,height/2+10);
                }
            }

            // 设置行
            rowDelegate:Rectangle
            {
                height:30;
                color: styleData.selected? "red":
                    (styleData.alternate ? tstyle.backgroundColor :
                                           tstyle.alternateBackgroundColor);
            }

            // 设置单元格
            itemDelegate: Text
            {
                text:styleData.value;
                font.pointSize:13;
                verticalAlignment:Text.AlignVCenter;
                horizontalAlignment:Text.AlignRight;
            }
        }

        TableViewColumn
        {
            role:"code";
            title:qsTr("Code");
            width:120;
            movable: false;
        }

        TableViewColumn
        {
            role:"name";
            title:qsTr("Name");
            width:120;
            movable: false;
        }

        ListModel {
              id: libraryModel
              ListElement {
                  code: "159922"
                  name: "500ETF"
              }
              ListElement {
                  code: "600030"
                  name: "中信证券"
              }
              ListElement {
                  code: "300244"
                  name: "迪安诊断"
              }
          }

        model: libraryModel;
    }
}

3. 访问和修改Model

(1) 访问数据

    itemDelegate: Text
            {
                text:styleData.value;
                font.pointSize:13;
                verticalAlignment:Text.AlignVCenter;
                horizontalAlignment:Text.AlignRight;

                MouseArea
                {
                    anchors.fill:parent;
                    onClicked:
                    {
                        console.log("currentRow:",styleData.row, "-", styleData.column);
                        console.log(libraryModel.get(styleData.row).code, "-",
                                    libraryModel.get(styleData.row).name);
                    }
                }
            }

(2)删除数据

    rowDelegate:Rectangle
            {
                height:30;
                color: styleData.selected? "red":
                    (styleData.alternate ? tstyle.backgroundColor :
                                           tstyle.alternateBackgroundColor);

                MouseArea
                {
                    anchors.fill:parent;
                    onClicked:
                    {
                        libraryModel.remove(styleData.row);
                    }
                }
            }

(3)修改数据

    itemDelegate: Text
            {
                text:styleData.value;
                font.pointSize:13;
                verticalAlignment:Text.AlignVCenter;
                horizontalAlignment:Text.AlignRight;

                MouseArea
                {
                    anchors.fill:parent;
                    onClicked:
                    {
                        console.log("currentRow:",styleData.row, "-", styleData.column);
                        console.log(libraryModel.get(styleData.row).code, "-",
                                    libraryModel.get(styleData.row).name);
                        libraryModel.set(styleData.row, {"code":"888888", "name":"modify"});
                    }
                }
            }

(4)添加数据

    rowDelegate:Rectangle
            {
                height:30;
                color: styleData.selected? "red":
                    (styleData.alternate ? tstyle.backgroundColor :
                                           tstyle.alternateBackgroundColor);

                MouseArea
                {
                    anchors.fill:parent;
                    onClicked:
                    {
                        //libraryModel.remove(styleData.row);
                        libraryModel.append({"code":"666666", "name":"add"});
                    }
                }
            }

 

相关推荐:

Qt设备调试的方法是什么

本篇内容主要讲解“Qt设备调试的方法是什么”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Qt设备调试的方法是什么”吧!一、前言设备调试核心就是将整个系统中的所有打印数据统一显示到一个模块上,一般都会将硬件通信的收发数据和对应的解析信号发出来或者qdebu...

Qt如何实现多重纹理

这篇文章给大家分享的是有关Qt如何实现多重纹理的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。可以将多个纹理传给shader,下图是两个纹理叠加的效果shader中定义如下:uniform sampler2D U_MainTexture; uniform...

QTextCodec头文件

没代码怎么回答,有可能没有包含QTextCodec头文件。评论(0)| 引用此答案| 举报 (2012-03-0817:36)0又失误了4年前这样添加可以吗?评论(0)| 引用此答案| 举报 (2012-03-0819:24)0szmneo...

QT QML的元素布局的实现

本文介绍QTQML跨平台移动APP开发中的元素布局的相关问题,先看一张图,我们来分析一下其中的问题: 这张图片中,有如下问题: 整体的布局没有居中显示 班级名称: 没有和请输入班级名称输入框垂直对齐 和输入框的距离太远 班主任的提示也一样 最后的Button一行,需求要求右对齐,在QML的程序中没有...

QML 可以多选ComboBox的实现

    由于项目需要,需要设计一个可以多选的ComboBox,看了一下文档,发现QML自带的ComboBox确实无法多选。看了一下ComboBox的实现,发现弹出的下拉菜单是用Menu实现的,看来只能自己重写了,毕竟Menu本身就是无法多选的!代码不多,直接看...

Qt自定义控件实现圆盘进度条

本文实例为大家分享了Qt圆盘进度条的具体代码,供大家参考,具体内容如下 自定义控件二:圆盘进度条 上效果图: 主要思路:使用qpainter根据图形需求画圆和圆弧,画指针(多边形,指定坐标即可),根据具体的value值旋转坐标系,使指针达到旋转效果,旋转度数是根据value值,总共360度,占比求得...

Qt自定义控件如何实现进度仪表盘

这篇文章主要介绍Qt自定义控件如何实现进度仪表盘,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!本文实例为大家分享了Qt自定义控件实现进度仪表盘的具体代码,供大家参考,具体内容如下先看效果图:思路:外围的线共100根(自定义,可改变),总共占270度,然后按照先画一条线然后旋转...

Qt自定义控件如何实现多彩色仪表盘

这篇文章主要介绍了Qt自定义控件如何实现多彩色仪表盘,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。Qt自定义控件4:多彩色仪表盘先看效果图:思路:外围三色的圆弧红:蓝:绿=1:2:1,总共占270度。刻度线是根据所在圆弧的颜色而画,刻...