qml tableview 数据库_Qt Quick之TableView的使用

这篇博客介绍了如何在Qt Quick中利用TableView组件显示和操作数据库数据。通过示例展示了如何设置TableView的样式,包括头部、行和单元格,并且提供了一个包含股票代码和名称的ListModel作为数据源。
摘要由CSDN通过智能技术生成

import QtQuick 2.9import QtQuick.Window2.2import QtQuick.Controls1.4import QtQuick.Controls.Styles1.4Window {

visible:truewidth:300height:480title: 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;

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值