QML<4>Tableview⾃定义表格显⽰delegateFontAwesomeCanvas QML <4> Tableview ⾃定义表格显⽰ delegate FontAwesome Canvas
前⾔
基于QML TableView 的⾃定义表格显⽰,表格实现类容参考⽹易云⾳乐如下图所⽰:
参考实现功能⾃定义表格实现如下:
⼀、表格列设置
表格列为固定列数,代码如下;
TableViewColumn{role:"num_flag";title:"";width: 80;elideMode: Text.ElideRight;}
TableViewColumn{role:"name";title:qsTr("标题");width: 320;elideMode: Text.ElideRight;}
TableViewColumn{role:"time";title:qsTr("时长");width: 90;}
TableViewColumn{role:"singer";title:qsTr("歌⼿");width: 120;}
⼆、headerDelegate
表头使⽤delegate⾃定义绘制,代码如下:
id: header_delegate
Rectangle{
height: 40
color: "white"
border.width: 1
Text{
id: headertext
text: styleData.value
color:"gray"
}
}
}
三、itemDelegate
Component
{
id:item_delegate
Loader
{
id:item_loader
anchors.fill: parent
// visible: true
//== 、=== 区别
//blog.csdn/weixin_42420703/article/details/82531125 sourceComponent:
{
var role = le;
if (role === "num_flag")
return num_falg_component;
if (role === "name")
return name_component;
el if (role === "time")
return time_component;
el if (role === "singer")
return singercomponent;
el return empty_component;
}
1.排⾏delegate
代码如下:
犬瘟热的治疗方法{
id:num_falg_component
Rectangle
{朝天门
id:draw_rect
height: row_height
property var row:"1";
路由器光信号闪红灯是怎么回事property var new_flag: fal;
//fal:down true:up
property var trend: true;
property var trend_num:11;
Canvas
{
id:draw
描写秋风的句子
anchors.fill: parent
onPaint:{
var ctx = getContext("2d");
row = w+1;
if(row === 2 )
{
new_flag = true;
}
el if( row == 3 )
{
trend = fal;
trend_num =6;
}
//⾏号
ctx.fillStyle = "gray";
ctx.lineWidth = 1;
ctx.font="12px Georgia";
ctx.fillText(row, 15,15,width/2);
//新
if(new_flag)
{
ctx.fillStyle = "green";
ctx.lineWidth = 3;
ctx.fillText("new", width/2,15,width/2);
}
el
{
var trend_text = String.fromCodePoint(0x2193); var trend_color="blue";
if(trend)
{
trend_color = "red";
trend_text = String.fromCodePoint(0xA71B);
}
ctx.fillStyle = trend_color;
ctx.fillText(trend_text, width/2,15,width/3);
ctx.fillStyle = "red";
ctx.fillText(trend_num, width/2+10,15,width/3*2);
}
}
}
}
}
2.标题delegate
标题栏主要实现功能是前三⾏会显⽰专辑图⽚,如图所⽰:
Component 代码如下:
Component
{
id:name_component
TableNameColumn
{
music_name_text:"As Long As You Love You"
image_show: w < 3 ? true:fal
iamge_source:"qrc:/img/img/109951166294262909.jpg"
height: row_height
width:parent.width
}
}
⾃定义组件TableNameColumn 代码如下:
import QtQuick 2.0
import QtQuick.Layouts 1.0
import QtQuick.Controls 2.12
import "qrc:/scripts/Custom/ToolScripts.js" as JsTool Rectangle
{
id:root
段姓
property string music_name_text:"";
property bool image_show: fal;
property alias iamge_source:image.source
width: 300
height: 50
anchors.fill: parent
Image{
id: image
width: 45
height: 45
anchors.verticalCenter: parent.verticalCenter
anchors.leftMargin: 0
anchors.left: parent.left
visible: image_show
}
Rectangle
短篇小说经典
{
id:music_name
width: 241
height: 45
anchors.left: image_show ?image.right :parent.left p: p
Button{
id: paly_canvas
background: Rectangle{
color:"white"
opacity: 0.2 //透明度
}
width: 18
height: 18
anchors.verticalCenter: parent.verticalCenter成人礼祝福
anchors.left: parent.left
font.family:"FontAwesome"
text:"\uf01d"
}
Canvas
{
id:name_draw
x: 26
y: 0
width: 222
height: 45
anchors.verticalCenter: parent.verticalCenter
anchors.left: paly_canvas.right
onPaint:
{
题海战术var ctx = getContext("2d");
ctx.fillStyle = "rgba(54, 54, 54, 1)";
ctx.lineWidth = 2;
ctx.font="11px Georgia";
ctx.fillText(music_name_text,1, height/2,height);
}
}
}
}