Qt Quick之TableView的使用

本博只是简单的展示TableView的基本使用(TableView、style:TableViewStyle、headerDelegate、rowDelegate、itemDelegate、Table

大家好,又见面了,我是全栈君,今天给大家准备了Idea注册码。

 本博只是简单的展示TableView的基本使用(TableView、style:TableViewStyle、headerDelegate、rowDelegate、itemDelegate、TableViewColumn、ListModel及访问和修改Model),关于更多属性和方法的使用可以参考TableView QML Type

1. 效果图

  Qt Quick之TableView的使用

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

3. 访问和修改Model

(1) 访问数据

    itemDelegate: Text
            {
                text:styleData.value;
                font.pointSize:13;
                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:30;
                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:13;
                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:30;
                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"});
                    }
                }
            }

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/120075.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 3DCNN

    3DCNN学习论文Appearance-and-RelationNetworksforVideoClassification的时候看到了3DCNN,因为是第一次接触视频分类,因此,查询学习转自:https://blog.csdn.net/zouxy09/article/details/9002508最近看DeepLearning的论文,看到这篇论文:3DConvolutionalNeural…

    2022年6月11日
    50
  • POI导出excel执行公式 公式不生效问题[通俗易懂]

    POI导出excel执行公式 公式不生效问题[通俗易懂]excel模板设置好公式即可。在下面这行代码:workbook.write(out);// 输出Excel内容,生成Excel文件 之前,添加这个语句:workbook.setForceFormulaRecalculation(true);// 执行公式。workbook.setForceFormulaRecalculation(true);// 执行公式workbook.write(out);// 输出Excel内容,生成Excel文件…

    2022年8月19日
    13
  • assertEquals方法

    assertEquals方法/***Assertsthattwoobjectsareequal.Iftheyarenot,an*{@linkAssertionError}isthrownwiththegivenmessage.If*expectedandactualarenull,*theyareconsideredeq

    2022年7月12日
    28
  • FEC介绍(一)_FEC项目是什么意思

    FEC介绍(一)_FEC项目是什么意思1、FEC是什么。FEC,forwarderrorcorrection,前向纠错。2、FEC用来做什么。网络数据传输时,难免出现数据包丢失等问题。数据传输出现丢包时,常用的传输方式有2种:丢包重传和前向纠错(FEC)。丢包重传是接收方发现数据包有丢失,请求发送方重新发包。帧头信息中的sequencenumber,正常状态下,该序列号是连续的。接收方根据该序列号判断是否丢包。可以看出,丢包重传是接收方和发送方本身有几次交互。FEC通过另外一种方式处理丢包。发送方在发送数据时,在每

    2022年8月11日
    8
  • jdbc是什么(jdbc五个步骤)

    测试工具:Pact、Janus、Pacto等。packagecom.example.demo;importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublicclassDemoApplication{publicstaticvoidmain(Strin

    2022年4月14日
    53
  • SVR回归_时间序列分析优缺点

    SVR回归_时间序列分析优缺点文章目录1.SVR时间序列预测2.SVR调参3.SVR高斯核与过拟合1.SVR时间序列预测SVR可用于时间序列分析,但不是较好的选择。现在一般采用LSTM神经网络来处理时间序列数据#SVR预测#也可用于时间序列分析(ARIMA也可用于时间序列分析)importnumpyasnpfromsklearnimportsvmimportmatplotlib.pyplotaspltif__name__==”__main__”:#构造数据N=50

    2025年7月29日
    2

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号