本博只是简单的展示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. Amr and Chemistry

    C. Amr and Chemistry time limit per test 1 second memory limit per test 256 megabytes input standard ...

  2. Nginx教程

    Nginx教程 1.背景 介绍 Nginx是一个高性能的HTTP服务器,以及反向代理服务器 组成 Ngnix有内核和模块组成.微结构的内核根据配置文件将一个请求映射到一个location块中,该loc ...

  3. Spring @Configuration注解

    1 该注解的用途 这个注解表示这个类可以作为spring ioc容器bean的来源,其本质上它是对xml文件中创建bean的一种替换.有了这个注释,Spring framework就能在需要的时候构造 ...

  4. BZOJ1791: [Ioi2008]Island 岛屿

    BZOJ1791: [Ioi2008]Island 岛屿 Description 你将要游览一个有N个岛屿的公园. 从每一个岛i出发,只建造一座桥. 桥的长度以Li表示. 公园内总共有N座桥. 尽管每 ...

  5. 我的设计模式学习笔记------>单例模式(Singleton)

    一.前言 有些时候,允许自由创建某个类的实例是没有意义,还可能造成系统性能下降(因为创建对象所带来的系统开销问题).例如整个Windows系统只有一个窗口管理器,只有一个回收站等.在Java EE应用 ...

  6. [Symfony\Component\Config\Definition\Exception\InvalidConfigurationException] The child node "db_driver" at path "fos_user" must be configured.

    $ php bin/console server:run [Symfony\Component\Config\Definition\Exception\InvalidConfigurationExce ...

  7. Java实现微信网页授权

    开发前的准备: 1.需要有一个公众号(我这里用的测试号),拿到AppID和AppSecret: 2.进入公众号开发者中心页配置授权回调域名.具体位置:接口权限-网页服务-网页账号-网页授权获取用户基本 ...

  8. 牛客小白月赛1 C 分元宵【快速幂】

    题目链接 https://www.nowcoder.com/acm/contest/85/C 思路 有 A 种 元宵馅,B 种元宵皮 所以 我们可以认为 有Q = A * B 种 元宵 有 C 张桌子 ...

  9. iOS 图文混排 链接 可点击

    对于这个话题 我想到 1 第一个解决方法就是使用 webView 比较经典 把所有复杂工作都交给控件本身去处理了,  但是好像好多需要自定义的地方 没法从 webView获得响应回调 :(估计也可以实 ...

  10. Java多线程系列 JUC线程池06 线程池原理解析(五)

    ScheduledThreadPoolExecutor解析 ScheduledThreadPoolExecutor适用于延时执行,或者周期性执行的任务调度,ScheduledThreadPoolExe ...