0

我有一个 ListView,他的模型来自 U1db.Query 从/到 U1db 的读取和保存操作有效,但是当在数据库中插入新项目以显示最后插入的内容时,我需要更新 ListView 模型(即:我需要执行相同的查询)。我也尝试过使用 ListView 和 UbuntuListView`,但我无法解决这个问题。当我“拉”列表以刷新它时,我收到错误:

Property 'update' of object U1db::Query(0xe097e0) is not a function

我查看了 sdk 文档,但没有找到任何有用的示例。感谢您的任何建议!

这里的片段(我省略了一些细节):

    import QtQuick 2.2
    import Ubuntu.Components 1.3
    import Ubuntu.Components.Popups 1.3

    import Ubuntu.Components.ListItems 1.3 as ListItem  //test

    import U1db 1.0 as U1db

//the delegate component used by the ListView
Component {
            id: peopleListDelegate

            Item {
                id: personItem                

                //draw a rectangle aroun the content to display 
                Rectangle {
                 //content omitted 
                }

                // This mouse region covers the entire delegate
                MouseArea {
                   //content omitted              
                }

                Row {
                    id: topLayout                    
                   //display some content to be shown inside the Rectangle
                 }
            }
        }


//the listView that show the DB Query result
        ListView  {

                         id: listView
                         anchors.fill: parent
                         model : allPeopleQuery
                         delegate: peopleListDelegate

                         highlight: highlightComponent                 
                         focus: true

                         Component{
                             id: listHeader

                             Item {
                                 width: parent.width
                                 height: units.gu(10)

                                 Text{
                                     anchors.bottom: parent.bottom
                                     anchors.horizontalCenter: parent.horizontalCenter
                                     text: i18n.tr("<b> Total people found: </b>") + listView.count
                                 }
                             }
                         }

                         header: listHeader

                         PullToRefresh  {                            
                                 refreshing: listView.model.status === ListModel.Loading
                                 onRefresh: listView.model.update()  // HERE THE PROBLEM, (also using reload() doesn't work)
                             }
                     }

        U1db.Database {
                id: mypeopleDb
               .....
            }

           U1db.Index{
               database: mypeopleDb
               id: all_field_index
               expression: .......
           }


          U1db.Query {
               id: allPeopleQuery
               index: all_field_index
               query: ["*", "*"]
           }
4

1 回答 1

0

经过几天的尝试,我找到了如何“解决”这个问题。具有 U1db 查询作为“模型”的 ListView 具有内置的模型自动刷新功能(例如:如果您在数据库中添加/删除项目并且查询的结果集更改,模型将被更新)。但是,在 AdaptiveLayout 中使用上面的代码,并使用如下语句在页面之间切换:

myAdaptiveLayout.addPageToNextColumn(aPage,Qt.resolvedUrl('AddPerson.qml'))

模型的自动刷新不起作用!

如果您在定义 AdaptiveLayout 的同一个 qml 文件中包含文件“AddPerson.qml”中的页面代码,则 ListView 模型自动刷新工作!

注意:我不知道这个解决方案是一种解决方法还是一个 qml 错误。

于 2016-07-12T13:34:23.247 回答