Qt Quick之TableView的使用
本博只是简单的展示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的使用的更多相关文章
- Qt 学习之路 :Qt Quick Controls
自 QML 第一次发布已经过去一年多的时间,但在企业应用领域,QML 一直没有能够占据一定地位.很大一部分原因是,QML 缺少一些在企业应用中亟需的组件,比如按钮.菜单等.虽然移动领域,这些组件已经变 ...
- Qt Quick Controls 与 Qt Quick Controls 2的区别(详细对照)
Qt Quick Controls 原本是为支持桌面平台而开发的,后来又加入了移动平台和嵌入式平台的支持.它们应用非常广泛,因为它们提供了足够灵活的样式系统,以允许开发具有平台相关或者无关风格的应用程 ...
- 《Qt Quick 4小时入门》学习笔记4
http://edu.csdn.net/course/detail/1042/14806?auto_start=1 Qt Quick 4小时入门 第七章:处理鼠标与键盘事件 1.处理鼠标事件 鼠标信号 ...
- 《Qt Quick 4小时入门》学习笔记3
http://edu.csdn.net/course/detail/1042/14807?auto_start=1 Qt Quick 4小时入门 第八章:Qt Quick中的锚(anchors)布局 ...
- 《Qt Quick 4小时入门》学习笔记2
http://edu.csdn.net/course/detail/1042/14805?auto_start=1 Qt Quick 4小时入门 第五章:Qt Quick基本界面元素介绍 1. ...
- 《Qt Quick 4小时入门》学习笔记
http://edu.csdn.net/course/detail/1042/14804?auto_start=1 Qt Quick 4小时入门 第五章:Qt Quick里的信号与槽 QML中 ...
- 从头学Qt Quick(3)-- 用QML写一个简单的颜色选择器
先看一下效果图: 实现功能:点击不同的色块可以改变文字的颜色. 实现步骤: 一.创建一个默认的Qt Quick工程: 二.添加文件Cell.qml 这一步主要是为了实现一个自定义的组件,这个组件就是我 ...
- 从头学Qt Quick(1) --体验快速构建动态效果界面
自2005年Qt4发布以来,Qt已经为成千上万的应用程序提供了框架服务,现在Qt已经基本上支持所有的开发平台了,这里面既包含了桌面.嵌入式领域,也包括了Android.IOS.WP等移动操作平台,甚至 ...
- Qt Quick实现的涂鸦程序
之前一直以为 Qt Quick 里 Canvas 才干够自绘.后来发觉不是,原来还有好几种方式都能够画图! 能够使用原始的 OpenGL(Qt Quick 使用 OpenGL 渲染).能够构造QSGN ...
随机推荐
- Laravel开发:Laravel框架门面Facade源码分析
前言 这篇文章我们开始讲 laravel 框架中的门面 Facade,什么是门面呢?官方文档: Facades(读音:/fəˈsäd/ )为应用程序的服务容器中可用的类提供了一个「静态」接口.Lara ...
- ANDROID版本号号和版本号名称的重要性介绍
转载请注明出处http://blog.csdn.net/y150481863/article/details/41249159,来自[http://blog.csdn.net/y150481863] ...
- DBCP组件概述
许多Apache的项目都支持与关系型数据库进行交互.为每个用户都创建一个连接是一项非常耗时的工作(通常情况下,需要花费几秒中),而为了执行数据库的事务处理可能还需要花费几毫秒.对于一个对外公开访问的互 ...
- api签名认证
参数列表: data: { sign, uid或是openId, version, timestamp, param } sign 签名一般情况下,根据如下几项生成,通过md5或是aes加密: 接口 ...
- python解释器安装教程
1. 首先,打开python的官网:python.org 2. 首页downloads下打开, 3. 最上边是两个最新的版本,长期计划,推荐使用python3,如果长期打算用p3,默认使用最新版本.如 ...
- spring web app的结构
1 入口是web.xml tomcat加载war的时候会去读该入库文件. 2 web.xml中spring mvc的配置 定义servlet到servlet-mapping之间的映射,org.spri ...
- Bootstrap 轮播图(Carousel)插件
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- wechat JS-SKD (getLoaction) 定位显示百度map
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...
- 关于date和String互相转换的问题
其实原理很简单,就是将String类型的变量使用SimpleDateFormat来转换成Date,然后用getTime()方法比较 SimpleDateFormat sdf = new SimpleD ...
- wap网站即手机端网页SEO优化注意事项及方法
定位和页面设计: 无论是PC端还是移动端,网站 都要考虑清楚消费群体的定位问题.虽然智能手机用户数量非常普及,但是要明白中国的大部分手机用户使用的还是2G网络,一直高 喊的3G.4G手机用户只有大约1 ...