地点列表 (QML)
地点列表示例演示了如何使用ListView 搜索和显示地点列表。
运行示例
要运行来自 Qt Creator,打开Welcome 模式,并从Examples 中选择示例。更多信息,请参阅Qt Creator: 教程:构建并运行。
Places List
示例演示了如何搜索某一地区的地点列表,并使用ListView 显示结果。在此特定情况下,要搜索的是与术语food
相关的地点。
执行地点搜索
要编写一个在列表中显示地点的 QML 应用程序,我们首先要做以下导入声明。
import QtQuick import QtPositioning import QtLocation
实例化一个Plugin 实例。Plugin 实际上是地方来源的后台。根据插件的类型,可能需要填写一些必填参数。最有可能的PluginParameter 类型是某种形式的服务访问令牌,这些令牌在服务插件中都有记录。在本代码段中使用的是osm
插件,它不需要任何其他参数:
Plugin { id: myPlugin name: "osm" //specify plugin parameters as necessary //PluginParameter {...} //PluginParameter {...} //... }
接下来,我们实例化一个PlaceSearchModel ,用来指定搜索参数和执行地点搜索操作。为便于说明,在模型构建完成后,将调用update() 。通常情况下,update() 会在响应用户操作(如点击按钮)时被调用。
PlaceSearchModel { id: searchModel plugin: myPlugin searchTerm: "food" searchArea: QtPositioning.circle(startCoordinate, 5000 /* 5 km radius */); Component.onCompleted: update() }
最后,我们实例化一个ListView 来显示模型找到的搜索结果。我们使用了一个内联委托,并假定每个搜索结果都是type PlaceSearchesult
。因此,我们假定总是可以访问role ,其他搜索结果类型可能没有role 。
© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.