在这篇文章里,我来介绍如何在Ubuntu OS上上面读取电话本的信息。
1)首先我们来创建一个最基本的应用
打开我们的Qt Creator, 我们来创建一个称作为“contact1"的项目。在本项目中,我们使用"App with Simple UI"的模版。我们修改我们的“main.qml"代码如下:
import QtQuick 2.0
import Ubuntu.Components 0.1
import "components"
import QtContacts 5.0
import Ubuntu.Components.ListItems 0.1 as ListItem
/*!
\brief MainView with a Label and Button elements.
*/
MainView {
// objectName for functional testing purposes (autopilot-qt5)
objectName: "mainView"
// Note! applicationName needs to match the "name" field of the click manifest
applicationName: "com.ubuntu.developer.liu-xiao-guo.contact1"
/*
This property enables the application to change orientation
when the device is rotated. The default is false.
*/
//automaticOrientation: true
// Removes the old toolbar and enables new features of the new header.
useDeprecatedToolbar: false
width: units.gu(100)
height: units.gu(75)
Page {
title: i18n.tr("Contact1")
ContactModel {
id: contactModel
manager: "galera"
}
ListView {
id: contactView
anchors {
left: parent.left
right: parent.right
top: parent.top
bottom: buttons.bottom
}
model: contactModel
delegate: ListItem.Subtitled {
text: contact.name.firstName
subText: contact.phoneNumber.number
}
}
}
}
这里我们首先输入我们的QtContact 5.0库。在代码中我们定义了如下的ContactModel:
ContactModel {
id: contactModel
manager: "galera"
}
一定要记住使用"galera" manager。最后,我们使用一个ListView来展示我们的内容。代码显示如上所示。但是我们现在马上在手机上运行时,可能看到如下的信息:
Fail to connect with service: QDBusError("org.freedesktop.DBus.Error.AccessDenied", "An AppArmor policy prevents this sender from sending this message to this recipient, 0 matched rules; type="method_call", sender=":1.119" (uid=32011 pid=20604 comm="/usr/lib/arm-linux-gnueabihf/qt5/bin/qmlscene $@ m") interface="org.freedesktop.DBus.Introspectable" member="Introspect" error name="(unset)" destination="com.canonical.pim" (uid=32011 pid=3057 comm="/usr/lib/arm-linux-gnueabihf/address-book-service/")")
这说明,我们的程序遇到了安全的问题。我们必须在我们应用的apparmor文件中加入相应的policy以来使得我们的读取是可行的。
==ou
加入"Contacts"policy后,我们再在手机中运行,可以看到如下的画面:
我们可以在如下的地址下载我们的代码:
bzr branch
lp:~liu-xiao-guo/debiantrial/contact1
2)读取favorite contact信息
我们在我们的应用中定义如下的以个favorite model
ContactModel {
id: favouritesContactsModel
manager: "galera"
sortOrders: [
SortOrder {
id: sortOrder
detail: ContactDetail.Name
field: Name.FirstName
direction: Qt.AscendingOrder
}
]
fetchHint: FetchHint {
detailTypesHint: [ContactDetail.Avatar,
ContactDetail.Name,
ContactDetail.PhoneNumber]
}
filter: DetailFilter {
id: favouritesFilter
detail: ContactDetail.Favorite
field: Favorite.Favorite
value: true
matchFlags: DetailFilter.MatchExactly
}
onErrorChanged: {
if (error) {
busyIndicator.busy = false
contactListView.error(error)
}
}
}
我们在手机上使一个contact成为favorite。这时在我们的ListView中使用我们的favorite Model。代码如下:
ListView {
id: contactView
anchors.fill:parent
model: root.showFavourites ? favouritesContactsModel : contactModel
delegate: ListItem.Subtitled {
text: contact.name.firstName
subText: contact.phoneNumber.number
}
}
我们可以看到我们的一个被设为favorite的contact被列举出来了。
代码在如下的地址可以下载:
bzr branch
lp:~liu-xiao-guo/debiantrial/contact2