【问题标题】:How to select row in TableView (qml) from C++?如何从 C++ 中选择 TableView (qml) 中的行?
【发布时间】:2014-10-14 08:42:30
【问题描述】:

我尝试使用 c++ 函数在我的 TableView 中选择单行。在文档中我找到了property "selection",但是当我尝试在我的代码中使用它时出现错误。

第一个问题。如何在我的 QML 代码中使用此属性?由于错误,这种方式不起作用:无效的属性分配:“选择”是只读属性

    TableView{
     ....
     selection.select(0)
    }

还有我的第二个问题。如何通过 c++ 更改此属性?我知道我必须使用 setProperty() 方法,但作为第二个参数将是 function: select(0)?

【问题讨论】:

    标签: c++ qt qml


    【解决方案1】:

    来自the documentation

    tableview.selection.select(0)       // select row index 0
    
    tableview.selection.select(1, 3)    // select row indexes 1, 2 and 3
    
    tableview.selection.deselect(0, 1)  // deselects row index 0 and 1
    
    tableview.selection.deselect(2)     // deselects row index 2
    

    你不能从 TableView 的定义中做到这一点。稍后您必须从 JavaScript 执行此操作。为您的 TableView 分配一个 ID,并使用该 ID,使用 JavaScript 选择适当的行。

    编辑:

    要从 C++ 中执行此操作,您首先需要获取该对象的句柄。最好的方法是将objectName 属性分配给该元素,然后使用QObject::findChild() 查找它。一旦获得指向 TableView 的指针,就可以调用它的方法,如 here 所述。实际上,您首先需要获取其selection 属性,然后调用其(selection's)select() 方法。

    【讨论】:

      猜你喜欢
      • 2021-04-20
      • 2017-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多