20200303-03 QML TableView(Qt5.12) 设置行/列表头

一、前言

  针对最新版本 TableView 如何设置表头,在新版中直接继承 Flickable 所以各项效果更加完备,由于需要频繁弹出和压入数据以保证资源的最大利用,官方建议不太适合静态类型的 delegate

       新版中,使用 index 进行数据索引,不再使用 styleData 关键字

二、示例

    TableView {
        anchors {
            top: parent.top
            bottom: bottomCheckGroup.top
            left: parent.left
            right: parent.right
            topMargin: 0
            leftMargin: 0
            rightMargin: 0
            bottomMargin: 2
        }

        id: tableView
        //QML 新出的设置行宽和列宽的方式,如果需要某一行隐藏,只需要将这一行返回值设置为 0 即可
        //具体看官方文档
        columnWidthProvider: function (column) { return __headWidth; }
        rowHeightProvider: function (column) { return 25; }


        leftMargin: rowsHeader.implicitWidth
        topMargin: columnsHeader.implicitHeight
        model: MyAdjustModel {

            id: table_model
        }


        ScrollBar.horizontal: ScrollBar{}
        ScrollBar.vertical: ScrollBar{}
        clip: true

        //用于关闭拖动效果
        boundsBehavior: Flickable.OvershootBounds
        
        delegate: SysTableViewDelegate {
        }
        //左上角内容自定义类以 Rectangle 
        SysTableLeftTopMaskDelegate {

        }
        //列头部 利用 Repeater 自动重复生成
        //SysTableTextColumnsHeaderDelegate 是本人自定义变量需要替换
        //新版 TableView 使用 index 进行数据索引        
        Row {
            id: columnsHeader
            y: tableView.contentY
            z: 2

            Repeater {
                model: tableView.columns > 0 ? tableView.columns : 1
                SysTableTextColumnsHeaderDelegate{
                    width: tableView.columnWidthProvider(modelData)
                    height: 65
                    color: __bordColor
                }
            }
        }
        //行头部
        Column {
            id: rowsHeader
            x: tableView.contentX
            z: 2
            Repeater {
                model: tableView.rows > 0 ?  tableView.rows : 1
                Rectangle {
                    width: 60
                    height: tableView.rowHeightProvider(modelData)
                    color: __bordColor
                    Label {
                         anchors.fill: parent
                         anchors {bottomMargin: 2}
                         anchors.centerIn: parent
                         text: table_model.headerData(modelData, Qt.Vertical)
                         color: '#7785a9'
                         font.pixelSize: 15
                         padding: 10
                         verticalAlignment: Text.AlignVCenter
                         horizontalAlignment: Text.AlignHCenter
                         background: Rectangle { color: "white" }
                    }
                }
            }
        }

        ScrollIndicator.horizontal: ScrollIndicator { }
        ScrollIndicator.vertical: ScrollIndicator { }
    }

三、图示

 

==9114== HEAP SUMMARY: ==9114== in use at exit: 14,336,980 bytes in 90,048 blocks ==9114== total heap usage: 428,295 allocs, 338,247 frees, 58,358,339 bytes allocated ==9114== ==9114== 0 bytes in 1 blocks are still reachable in loss record 1 of 25,502 ==9114== at 0x4C3289F: operator new[](unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) ==9114== by 0x66F9EBA: QV4::CompiledData::CompilationUnit::linkToEngine(QV4::ExecutionEngine*) (in /opt/Qt5.12.9/5.12.9/gcc_64/lib/libQt5Qml.so.5.12.9) ==9114== by 0x6904054: QQmlObjectCreator::init(QQmlContextData*) (in /opt/Qt5.12.9/5.12.9/gcc_64/lib/libQt5Qml.so.5.12.9) ==9114== by 0x6904202: QQmlObjectCreator::QQmlObjectCreator(QQmlContextData*, QQmlRefPointer<QV4::CompiledData::CompilationUnit> const&, QQmlContextData*, QQmlIncubatorPrivate*) (in /opt/Qt5.12.9/5.12.9/gcc_64/lib/libQt5Qml.so.5.12.9) ==9114== by 0x688E2E1: QQmlComponentPrivate::beginCreate(QQmlContextData*) (in /opt/Qt5.12.9/5.12.9/gcc_64/lib/libQt5Qml.so.5.12.9) ==9114== by 0x688BE48: QQmlComponent::create(QQmlContext*) (in /opt/Qt5.12.9/5.12.9/gcc_64/lib/libQt5Qml.so.5.12.9) ==9114== by 0x4E49D6E: QQuickWidget::continueExecute() (in /opt/Qt5.12.9/5.12.9/gcc_64/lib/libQt5QuickWidgets.so.5.12.9) ==9114== by 0x4E4A18A: QQuickWidgetPrivate::execute() (in /opt/Qt5.12.9/5.12.9/gcc_64/lib/libQt5QuickWidgets.so.5.12.9) ==9114== by 0x1397D2: TWDZVirtualKeyboard::TWDZVirtualKeyboard() (twdzvirtualkeyboard.cpp:24) ==9114== by 0x13965E: TWDZVirtualKeyboard::GetInstance() (twdzvirtualkeyboard.cpp:8) ==9114== by 0x136629: UartTest::UartTest(int, int, QWidget*) (uarttest.cpp:18) ==9114== by 0x136157: communicationModule::initUi() (communicationmodule.cpp:11)
06-09
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值