QML TableView rowDelegate styleData.row 未定义 [英] QML TableView rowDelegate styleData.row not defined

查看:80
本文介绍了QML TableView rowDelegate styleData.row 未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为 TableView 创建自定义行委托.根据

的值

我的代码很简单:

TableView {宽度:500//调试高度:300//调试模型:列表模型{列表元素{讲座名称:Baum1"}列表元素{讲座名称:Baum2"}列表元素{讲座名称:Baum3"}列表元素{讲座名称:Baum4"}}rowDelegate: HeaderRowDelegate {//只是一个带有名为modelRow"的属性的矩形id:rowDelegate模型行:{var 数据 = 样式数据;返回数据行;}}表视图列{编号:c1角色:讲座名称"书名:《测试》}}

解决方案

您不必自己分配:因为 HeaderRowDelegate 组件已分配给 rowDelegateTableView 的属性,您的组件已经可以访问 styleData.row 属性.

这是一个例子:

ma​​in.qml

导入QtQuick 2.4导入 QtQuick.Layouts 1.1导入 QtQuick.Controls.Styles 1.2导入 QtQuick.Controls 1.3应用程序窗口{编号:应用程序标题:qsTr("测试")宽度:800高度:600表视图{宽度:500//调试高度:300//调试模型:列表模型{列表元素{讲座名称:Baum1"}列表元素{讲座名称:Baum2"}列表元素{讲座名称:Baum3"}列表元素{讲座名称:Baum4"}}rowDelegate: RowDel {}表视图列{编号:c1角色:讲座名称"书名:《测试》}}}

现在是行委托,RowDel.qml

导入QtQuick 2.4长方形 {id:rowDel颜色:蓝色"身高:60只读属性 int modelRow: styleData.row ?styleData.row : 0鼠标区域{anchors.fill:父级已点击:{console.log("[!] 日志:" + modelRow);}}}

这里重要的是你可以直接从你的组件中引用 styleData.row(显然,只要你使用这个精确的组件作为行委托).

例如,如果您单击每个 tableview 行,您应该会在控制台日志中看到正确的行号:

qml: [!] 日志: 0qml: [!] 日志: 1qml: [!] 日志: 2qml: [!] 日志: 3

I am trying to create a custom row delegate for a TableView. according to the Documentation, the rowDelegate should have access to a property called styleData.row. However, when I'm try to access this property, it is undefined. I used the debugger to check whats inside the styleData, and the row is missing:

My code is very simple:

TableView {
    width: 500//DEBUG
    height: 300//DEBUG

    model: ListModel {
        ListElement {
            lectureName: "Baum1"
        }
        ListElement {
            lectureName: "Baum2"
        }
        ListElement {
            lectureName: "Baum3"
        }
        ListElement {
            lectureName: "Baum4"
        }
    }

    rowDelegate: HeaderRowDelegate {//simply a Rectangle with an in property called "modelRow"
        id: rowDelegate
        modelRow: {
            var data = styleData;
            return data.row;
        }
    }

    TableViewColumn {
        id: c1
        role: "lectureName"
        title: "TEST"
    }
}

解决方案

You don't have to assign it yourself: as the HeaderRowDelegate component is assigned to the rowDelegate property of the TableView, your component has already access to the styleData.row property.

Here's an example:

main.qml

import QtQuick 2.4
import QtQuick.Layouts 1.1
import QtQuick.Controls.Styles 1.2
import QtQuick.Controls 1.3

ApplicationWindow {
    id: app
    title: qsTr("Test")
    width: 800
    height: 600

    TableView {
        width: 500//DEBUG
        height: 300//DEBUG

        model: ListModel {
            ListElement {
                lectureName: "Baum1"
            }
            ListElement {
                lectureName: "Baum2"
            }
            ListElement {
                lectureName: "Baum3"
            }
            ListElement {
                lectureName: "Baum4"
            }
        }

        rowDelegate: RowDel {}        

        TableViewColumn {
            id: c1
            role: "lectureName"
            title: "TEST"
        }
    }
}

Now the row delegate, RowDel.qml

import QtQuick 2.4

Rectangle {
    id: rowDel
    color: "blue"
    height: 60

    readonly property int modelRow: styleData.row ? styleData.row : 0

    MouseArea {
        anchors.fill: parent
        onClicked: {
            console.log("[!] log: " + modelRow);
        }
    }       
}

The important thing here is that you can reference styleData.row directly from your component (obliviously as long as you use this precise component as a row delegate).

As an example, if you click on each tableview row, you should see the correct row number displayed in the console log:

qml: [!] log: 0
qml: [!] log: 1
qml: [!] log: 2
qml: [!] log: 3

这篇关于QML TableView rowDelegate styleData.row 未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆