本博只是简单的展示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. PHP fsockopen模拟POST/GET方法

    原文链接:http://www.nowamagic.net/academy/detail/12220214 fsockopen 除了前面小节的模拟生成 HTTP 连接之外,还能实现很多功能,比如模拟  ...

  2. 大数进制转换 poj1220

    普通的做法,大数除小数. 复杂度o( log(n)*log(n) ),其实就是位数的平方. NUMBER BASE CONVERSION Time Limit: 1000MS   Memory Lim ...

  3. KPI、KPA、OKR三者的区别

    KPI.KPA或者OKR并不是水火不相容有你无我的概念,针对不对的业务状态.管理模式应该有所选择.以下是介绍它们之间的区别. 什么是KPI关键绩效指标 KPI(key performance indi ...

  4. iOS main函数讲解

    int main(int argc, char * argv[]) { @autoreleasepool { //四个参数 主要讲解后面两个参数 /* 第三个参数:UIApplication或者其子类 ...

  5. 20179209《Linux内核原理与分析》第十一周作业

    Nmap配合Metasploit进行端口扫描 1.Nmap扫描器基本使用 1.1简介 Nmap(Network Mapper)最早是Linux下的网络扫描嗅探器.其基本功能有三个: 探测一组主机是否在 ...

  6. JSP 分页代码

    jsp 分页模板 后台分页代码: 说明: 在 com.zc.domain 包下: PageBean.java 文件 package cn.itcast.customer.domain;   impor ...

  7. 【Android】开发优化之——调优工具:dump hprof file 查看内存情况,找到内存泄露

    虽说知道一般性的开发android应用须要注意的问题,但是也有水平參差不齐的情况.特别是维护代码,假设内存占用大,内存溢出严重,又怎么解决呢?  --  通过DDMS把heap抓出来分析 1.打开DD ...

  8. Module 'curl' already loaded in Unknown on line 0

    Module 'curl' already loaded in Unknown on line 0 应该是php binary已经包含curl,你又动态加载了一遍.屏蔽掉你的extension 引用, ...

  9. 短时程突触可塑性(short-term synaptic plasticity)

    介绍 神经元的突触可塑性一般被认为是大脑学习与记忆的分子生物学机制,它是指突触传递效率增强或减弱的变化现象.若这种变化只持续数十毫秒到几分,便称之为短时程突触可塑性,其中效率增强与减弱分别叫做短时程增 ...

  10. ALV行 列颜色设置

    ALV的颜色设置分为3种:行.列.单元格.   1.列颜色的设置   在 slis_t_fieldcat_alv-emphasize 中,写入需要的颜色代码.   Eg:   DATA: fc TYP ...