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

1. 效果图

  

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:
height:
title: qsTr("Hello World") TableView
{
id:stockTable; anchors.left: parent.left;
anchors.top:parent.top;
anchors.topMargin: ;
anchors.right: parent.right;
anchors.bottom: parent.bottom;
//alternatingRowColors : true;
style:TableViewStyle
{
id:tstyle;
backgroundColor:"white";
alternateBackgroundColor:"#f6F6F6";
textColor:"black"; // 设置TableView的头部
headerDelegate: Canvas
{
implicitWidth:;
implicitHeight:; onPaint:
{
var ctx = getContext("2d");
ctx.lineWidth = ;
ctx.strokeStyle="red";
ctx.fillStyle="blue"; ctx.beginPath();
console.log("width:",width,"--","height:",height);
ctx.rect(,,width,height);
ctx.stroke();
ctx.font="14pt sans-serif";
ctx.textAlign="right"
ctx.textBaseLine="middle";
ctx.fillText(styleData.value,width-,height/+);
}
} // 设置行
rowDelegate:Rectangle
{
height:;
color: styleData.selected? "red":
(styleData.alternate ? tstyle.backgroundColor :
tstyle.alternateBackgroundColor);
} // 设置单元格
itemDelegate: Text
{
text:styleData.value;
font.pointSize:;
verticalAlignment:Text.AlignVCenter;
horizontalAlignment:Text.AlignRight;
}
} TableViewColumn
{
role:"code";
title:qsTr("Code");
width:;
movable: false;
} TableViewColumn
{
role:"name";
title:qsTr("Name");
width:;
movable: false;
} ListModel {
id: libraryModel
ListElement {
code: ""
name: "500ETF"
}
ListElement {
code: ""
name: "中信证券"
}
ListElement {
code: ""
name: "迪安诊断"
}
} model: libraryModel;
}
}

3. 访问和修改Model

(1) 访问数据

    itemDelegate: Text
{
text:styleData.value;
font.pointSize:;
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:;
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:;
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:;
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 Quick之TableView的使用的更多相关文章

  1. Qt 学习之路 :Qt Quick Controls

    自 QML 第一次发布已经过去一年多的时间,但在企业应用领域,QML 一直没有能够占据一定地位.很大一部分原因是,QML 缺少一些在企业应用中亟需的组件,比如按钮.菜单等.虽然移动领域,这些组件已经变 ...

  2. Qt Quick Controls 与 Qt Quick Controls 2的区别(详细对照)

    Qt Quick Controls 原本是为支持桌面平台而开发的,后来又加入了移动平台和嵌入式平台的支持.它们应用非常广泛,因为它们提供了足够灵活的样式系统,以允许开发具有平台相关或者无关风格的应用程 ...

  3. 《Qt Quick 4小时入门》学习笔记4

    http://edu.csdn.net/course/detail/1042/14806?auto_start=1 Qt Quick 4小时入门 第七章:处理鼠标与键盘事件 1.处理鼠标事件 鼠标信号 ...

  4. 《Qt Quick 4小时入门》学习笔记3

    http://edu.csdn.net/course/detail/1042/14807?auto_start=1 Qt Quick 4小时入门 第八章:Qt Quick中的锚(anchors)布局 ...

  5. 《Qt Quick 4小时入门》学习笔记2

    http://edu.csdn.net/course/detail/1042/14805?auto_start=1   Qt Quick 4小时入门 第五章:Qt Quick基本界面元素介绍   1. ...

  6. 《Qt Quick 4小时入门》学习笔记

    http://edu.csdn.net/course/detail/1042/14804?auto_start=1   Qt Quick 4小时入门 第五章:Qt Quick里的信号与槽   QML中 ...

  7. 从头学Qt Quick(3)-- 用QML写一个简单的颜色选择器

    先看一下效果图: 实现功能:点击不同的色块可以改变文字的颜色. 实现步骤: 一.创建一个默认的Qt Quick工程: 二.添加文件Cell.qml 这一步主要是为了实现一个自定义的组件,这个组件就是我 ...

  8. 从头学Qt Quick(1) --体验快速构建动态效果界面

    自2005年Qt4发布以来,Qt已经为成千上万的应用程序提供了框架服务,现在Qt已经基本上支持所有的开发平台了,这里面既包含了桌面.嵌入式领域,也包括了Android.IOS.WP等移动操作平台,甚至 ...

  9. Qt Quick实现的涂鸦程序

    之前一直以为 Qt Quick 里 Canvas 才干够自绘.后来发觉不是,原来还有好几种方式都能够画图! 能够使用原始的 OpenGL(Qt Quick 使用 OpenGL 渲染).能够构造QSGN ...

随机推荐

  1. 从零开始写一个Exporter

    前言 上一篇文章中已经给大家整体的介绍了开源监控系统Prometheus,其中Exporter作为整个系统的Agent端,通过HTTP接口暴露需要监控的数据.那么如何将用户指标通过Exporter的形 ...

  2. [转]linux terminal中使用proxy

    转自:http://www.cnblogs.com/JoJosBizarreAdventure/p/5892383.html 在linux terminal中使用代理 方法一: terminal中输入 ...

  3. 最简单的 GitExtensions 教程(持续更新中)

    一.安装 GitExtensions 下载 GitExtensions 完全版,一直点 Next,安装全部组件. 二.将项目文件夹/文件提交到 Git 服务器(以 GitHub 为例) 新建一个文件夹 ...

  4. Django之表单验证

    对于前端的表单进行验证的方法,从最简单的js到基于XML传输的Ajax,再到cookie的免认证,现在Django为我们提供了自带的表单验证方法. views.py: from django impo ...

  5. java使用poi读写Excel

    package com.demo.excel; import com.demo.pojo.Student; import org.apache.poi.hssf.usermodel.HSSFCell; ...

  6. 排序算法-python版

    总结了一下常见集中排序的算法 归并排序 归并排序也称合并排序,是分治法的典型应用.分治思想是将每个问题分解成个个小问题,将每个小问题解决,然后合并. 具体的归并排序就是,将一组无序数按n/2递归分解成 ...

  7. linux基础part2

    linux基础 一.linux基础命令 1.pwd:用来显示当前目录位置 2.cd:用来切换目录位置.(eg:cd...cd../...cd-.cd~) 3.ls:用来查看目录或文件信息(eg:ls ...

  8. CAS的实现Atomic类库

    atomic 原子(atomic)本意是"不能被进一步分割的最小粒子",而原子操作(atomic operation)意为"不可被中断的一个或一系列操作".在多 ...

  9. IOS 代码风格习惯 总结1

    从我大三下学期开始工作开始, 几乎都是孤独的开发  因为身边开发ios 不多 ,除了学习开源的代码优秀风格技巧 剩下的 就是自己造, 所以 养成了 好多不好的习惯. 本知道面向对象的好处 ,但是实际开 ...

  10. 继承、多态——成员变量、成员函数、构造函数(this、super)

    继承 1.继承使用原因: 1.提高了代码的复用性 2.让类与类之间产生了关系,有了这个关系,才有了多态的特性 2.继承注意事项: 千万不要为了获取其他类的功能,简化代码而继承. 必须是类与类之间有所属 ...