簡體   English   中英

如何使用 QML 中的 TableView 從 QtQuickControls 1 獲取列數據?

[英]How to get column data from QtQuickControls 1 with TableView in QML?

我使用import QtQuick.Controls 1.4 as CC 每當我點擊第一列時,我得到的輸出是未定義的,而我能夠獲得其余列的數據。

第一列有一個委托。
從具有委托的第一列獲取數據的方法是什么?

import QtQuick.Window 2.12
import QtQuick 2.12
import QtQuick.Controls 1.4 as CC
import QtQuick.Controls.Styles 1.4

Window
{
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")

    CC.TableView
    {
        height: 400; width: 600

        style: TableViewStyle
        {
            headerDelegate: Rectangle
            {
                height: 20
                color: "lightsteelblue"
                Text
                {
                    width: parent.width
                    text: styleData.value
                }
            }


            rowDelegate: Rectangle
            {
                color: "blue"
                height: 30

                MouseArea
                {
                    id: ma
                    anchors.fill: parent
                    onClicked:
                    {
                        console.log(styleData.value)
                    }
                }

            }

            itemDelegate: Rectangle
            {
                color: "blue"
                height: 30

                Text
                {
                    text: styleData.value
                }

                MouseArea
                {
                    id: ma1
                    anchors.fill: parent
                    onClicked:
                    {
                        console.log(styleData.value)
                    }
                }

            }
        }

        CC.TableViewColumn
        {
            role: "aaa"
            title: "AAA"
            width: 100

            delegate: Item
            {
                Rectangle
                {
                    anchors.left: parent.left
                    id: pic
                    radius: 100
                    height: 15; width: 15; color: "red"
                }

                Text
                {
                    anchors.left: pic.right
                    anchors.leftMargin: 10
                    text: styleData.value
                }
            }
        }
        CC.TableViewColumn
        {
            role: "bbb"
            title: "BBB"
            width: 100
        }

        CC.TableViewColumn
        {
            role: "ccc"
            title: "CCC"
            width: 100
        }

        model: ListModel
        {
            id: mymodel
            ListElement
            {
               aaa : "Banana1"
               bbb : "Apple1"
               ccc : "zzz1"
            }
            ListElement
            {
                aaa : "Banana2"
                bbb : "Apple2"
                ccc : "zzz2"
            }
        }
    }
}

對於舊的(編輯過的)問題:
您應該在控制台日志行中使用styleData.row而不是styleData.value

對於新問題:
您的列有一個委托,它會覆蓋表的項目委托。 然后,當點擊第一列時,實際上是調用控制台日志的行的委托。

您可以通過將列委托更改為:

CC.TableViewColumn
{
    role: "aaa"
    title: "AAA"
    width: 100

    delegate: Item
    {
        Rectangle
        {
            anchors.left: parent.left
            id: pic
            radius: 100
            height: 15; width: 15; color: "red"
        }

        Text
        {
            id: text_id
            anchors.left: pic.right
            anchors.leftMargin: 10
            text: styleData.value
        }
        MouseArea
        {
            id: ma2
            anchors.fill: parent
            onClicked: {

                console.log(text_id.text)
            }
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM