【问题标题】:How to get element position in QML Controls 2 TableView如何在 QML Controls 2 TableView 中获取元素位置
【发布时间】:2019-05-12 18:48:01
【问题描述】:

刚开始使用 QML,决定使用最新的 QtQuick 和 Controls 2,但由于在 TableView 中获取元素位置而陷入困境。 这是我的 ma​​in.qml:

import QtQuick 2.12
import QtQuick.Controls 2.12

            TableView {
                id: tview
                width: 272
                height: 200
                columnSpacing: 1
                rowSpacing: 1
                model: tablemodel
                          delegate: Rectangle {
                              implicitWidth: genrecell.implicitWidth
                              implicitHeight: 30
                              border.color: "black"
                              border.width: 0.2

                              Text {
                                  id: genrecell
                                  text: tabledata
                                  font.pointSize: 12
                                  leftPadding: 5
                                  rightPadding: 5
                                  anchors.verticalCenter: parent.verticalCenter
                              }
                              ItemDelegate {
                                      id: idel1
                                      width: parent.width
                                      onClicked: console.log(genrecell.text) // i could get text from element, but same element's could be couple
                                  }
                          }
                ScrollBar.vertical: ScrollBar {
                    position: 1.0
                    width: 10
                }
            }

作为模型 i 使用 qt 建议 QAbstractTableModel 类 在 tablemodel.cpp:

TableModel::TableModel(QObject *parent) : QAbstractTableModel(parent)

如果这里有任何获取元素位置的可能性,或者我必须切换到 Controls 1 TableView?

【问题讨论】:

  • 该模型提供了几个属性,请参阅 Qt 文档: 委托提供了一个模板,用于定义视图实例化的每个单元格项目。模型索引作为可访问的索引属性公开。这同样适用于行和列。
  • @folibis 看到了,但找不到解决方案或示例

标签: qt qml tableview


【解决方案1】:

在我的 Text {} 中使用了这个。它记录行和列:

                MouseArea{
                    anchors.fill: parent
                    onClicked: {
                        console.log( row, column, index)
                    }
                }

【讨论】:

    猜你喜欢
    • 2017-09-11
    • 2017-07-23
    • 2019-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-26
    相关资源
    最近更新 更多