为了账号安全,请及时绑定邮箱和手机立即绑定

如何使用 QML 中的 TableView 从 QtQuickControls 1 获取列数据?

如何使用 QML 中的 TableView 从 QtQuickControls 1 获取列数据?

慕田峪4524236 2023-08-18 16:56:13
我正在使用import QtQuick.Controls 1.4 as CC. 每当我单击第一列时,我得到的输出是未定义的,而我能够获取其余列的数据。第一列有一个代表。从具有委托的第一列获取数据的方法是什么?import QtQuick.Window 2.12import QtQuick 2.12import QtQuick.Controls 1.4 as CCimport QtQuick.Controls.Styles 1.4Window{    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)                    }                }            }        }
查看完整描述

1 回答

?
FFIVE

TA贡献1797条经验 获得超6个赞

对于旧的(编辑过的)问题:您应该在控制台日志行中

使用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)

            }

        }

    }

}


查看完整回答
反对 回复 2023-08-18
  • 1 回答
  • 0 关注
  • 76 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信