1

我拥有的是一个列表视图,用于显示联系人列表。此外,还有一个搜索框和一个标签。根据在搜索框中键入的文本,将过滤联系人列表。

这是我的代码:

import QtQuick 1.1
import com.nokia.symbian 1.1
import com.nokia.extras 1.1
import QtMobility.contacts 1.1

Window {
id: window

StatusBar {
    id: statusBar
    anchors.top: window.top
    Text {
        id: statusBarTitle
        text: "Contacts"
        color: "#ffffff"
    }
}

ContactModel {
    id: contactModel

    filter:
        IntersectionFilter {
        DetailFilter {
        detail: ContactDetail.PhoneNumber
        field: PhoneNumber.PhoneNumber
        value: PhoneNumber.Mobile
        }

        UnionFilter {
            DetailFilter {
            detail: ContactDetail.Name
            field: Name.FirstName
            value: searchbox.searchText
            matchFlags: Filter.MatchStartsWith
            }

            DetailFilter {
            detail: ContactDetail.Name
            field: Name.LastName
            value: searchbox.searchText
            matchFlags: Filter.MatchStartsWith
            }

            DetailFilter {
            detail: ContactDetail.DisplayLabel
            field: DisplayLabel.Label
            value: searchbox.searchText
            matchFlags: Filter.MatchStartsWith
            }
        }
    }

    sortOrders:
        SortOrder {
        detail: ContactDetail.Name
        field: Name.FirstName
        direction: Qt.AscendingOrder
    }
}

Component {
    id: contactListDelegate
    ListItem {
        id: listItem

        Image {
            id: avatar
            source: contact.thumbnail
            sourceSize.width: 60
            sourceSize.height: 60
            anchors.verticalCenter: parent.verticalCenter
            anchors.left: parent.left
        }

        ListItemText {
            id: nameText
            text: contact.name.firstName + " " + contact.name.lastName
            color: "white"
            anchors.left: avatar.right
            anchors.leftMargin: 10
            font.family: "Helvetica"
            anchors.verticalCenter: parent.verticalCenter
        }
    }
}

ListView {
    id: listView
    anchors.top: searchbox.bottom
    anchors.right: parent.right
    anchors.left: parent.left
    anchors.bottom: toolBar.top
    clip: true
    model: contactModel
    delegate: contactListDelegate
    visible: true
}

SearchBox {
    id: searchbox
    anchors.top: statusBar.bottom
    placeHolderText: "Search Contact"

    onSearchTextChanged: {
        searchingBusyIndicator.running = true
        searchingBusyIndicator.visible = true
        searchTimeoutTimer.restart()
    }
}

Label {
    id: noMatchesLabel
    anchors.centerIn: listView
    visible: false
    text: "No matches"
}

BusyIndicator{
    id: searchingBusyIndicator
    anchors.centerIn: parent
    platformInverted: window.platformInverted
    width: 80
    height: 80
    visible: false
}

Timer{
    id: searchTimeoutTimer
    interval: 1000

    onTriggered: {
        searchingBusyIndicator.running = false
        searchingBusyIndicator.visible = false
    }
}

ToolBar {
    id: toolBar
    anchors.bottom: window.bottom
    tools: ToolBarLayout {
        id: toolBarLayout
        ToolButton {
            flat: true
            iconSource: "toolbar-back"
            onClicked: {
                Qt.quit()
            }
        }
        ToolButton {
            flat: true
            iconSource: "toolbar-search"
            }
        }
    }
}
}

现在,当列表中没有匹配项时,我需要将列表视图的可见属性更改为 false 并将标签属性更改为 true。如何做到这一点。?

现在发生的情况是,应用程序在没有匹配项时崩溃并终止。

4

1 回答 1

0

我花了 2 天时间寻找这个问题的解决方案......

这是我的修复:

ContactModel {
     ...........
     onRowsInserted: {
     if(last === 0)
     {
         noMatchesLabel.visible = true;
         listView.model = {};
     }
     else
     {
         noMatchesLabel.visible = false;
         listView.model = contactModel;
     }
}

虽然由于使用过滤器,contactModel 没有联系人,但我们将 listView 的模型交换为空的。这样应用程序就不会崩溃

于 2013-06-03T14:56:55.543 回答