简体   繁体   English

在可扩展QML应用程序中访问ListView索引

[英]Accessing listview index in an extensible QML application

I'm doing an extensible QML application, it means that some of the components are loaded according to the "plugins" loaded on the application. 我正在做一个可扩展的QML应用程序,这意味着某些组件是根据应用程序上加载的“插件”加载的。

One of the situations is the following. 一种情况是以下情况。

I've a ListView, and some of the elements shown on the ListView delegate are loaded based on the plugins. 我有一个ListView,ListView委托上显示的某些元素是基于插件加载的。 To create the component/object I use the functions: Qt.createComponent and component.createObject 要创建组件/对象,请使用以下函数: Qt.createComponent 和component.createObject

It works great, and I can see and interact with the qml components loaded. 它工作得很好,我可以看到所加载的qml组件并与之交互。

However, for some of the features I need to change the listview index from inside a plugin qml file (that is loaded on a delegated), but the index is not accessible from there, and I get a simple index is not defined When trying to. 但是,对于某些功能,我需要从插件qml文件(已加载到委托的文件)中更改listview索引,但是无法从那里访问该索引,并且在尝试执行以下操作时,我未定义一个简单的索引

The code is on github: The extension file is this one: https://github.com/danielfranca/procrastinationkiller/blob/master/extensions/timerTasks/taskRow.qml 代码在github上:扩展文件是这样的: https : //github.com/danielfranca/procrastinationkiller/blob/master/extensions/timerTasks/taskRow.qml

The specific part I need to access the index is on the clicked signal of mousearea of element with id playpause . 我需要访问索引中的特定部分是ID为playpause元素的鼠标区域的点击的信号。

I also tried finding the ListView element, and calling the function indexAt with the x, y mouse coordinates, but it's always returning 0. 我还尝试查找ListView元素,并使用x,y鼠标坐标调用函数indexAt ,但它始终返回0。

The specific code that loads the component is this one: 加载组件的特定代码是以下代码:

Component.onCompleted: {
                        addTaskLayout.inputObjects = Extensions.createExtensionComponent("extraInput.qml", addTaskLayout);
                    }

And this function is a javascript function that is in this file: https://github.com/danielfranca/procrastinationkiller/blob/master/extensions.js 该函数是此文件中的javascript函数: https : //github.com/danielfranca/procrastinationkiller/blob/master/extensions.js

Let me know if you need more details, and I would love to understand why QML don't expose the index to loaded components and why it can't find the index even with x,y coordinates. 让我知道是否需要更多详细信息,并且我想了解为什么QML不会将索引公开给加载的组件,以及为什么即使使用x,y坐标也找不到索引。

I'm using Qt5.4, Ubuntu 14.10, Qt Creator 3.3.1 我正在使用Qt5.4,Ubuntu 14.10,Qt Creator 3.3.1

The solution is simpler than I expected. 该解决方案比我预期的要简单。 The best way seems to be sending the index as a parameter when creating the component. 最好的方法似乎是在创建组件时将索引作为参数发送。 In my case the call became: 就我而言,电话变成:

Extensions.createExtensionComponent("taskRow.qml", row, {"model":tasksModel.get(index), "index": index});

And in my plugin component I create a property with the name index as follow: 然后在我的插件组件中创建一个具有名称索引的属性,如下所示:

property var index: null

Now in my extension function I expect an object with a variable number of properties, and I set it in the creation of the qml element. 现在,在扩展函数中,我希望对象具有可变数量的属性,并在创建qml元素时对其进行设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM