Get the mobile number from a contact in a list component
-
I tried to create a list view that shows only the mobile phone numbers of my contacts. So i declared a detail filter that allows only contacts with mobile numbers. This works fine.
But in the component of the list view i can not differ between mobile and other phone numbers, so always the first number of the contact is shown. How can I show only mobile numbers? I have no idea how this can be done in QML.@ListView {
id: mainList
anchors.fill: parent
clip: true
model: contactModel.contacts
delegate: listDelegateContactModel { id: contactModel filter: DetailFilter { detail: ContactDetail.PhoneNumber field: PhoneNumber.PhoneNumber value: PhoneNumber.Mobile } sortOrders: [ SortOrder { detail: ContactDetail.Name field: Name.LastName direction: Qt.AscendingOrder }, SortOrder { detail: ContactDetail.Name field: Name.FirstName direction: Qt.AscendingOrder } ] } Component { id: listDelegate ListItem { id: listItem Column { anchors.fill: listItem.paddingItem ListItemText { id: nameItem mode: listItem.mode role: "Title" text: displayLabel } ListItemText { id: numberItem mode: listItem.mode role: "SubTitle" text: phoneNumber.number //<- here i want to get the mobile number, not just the first available number } } } } ScrollDecorator { flickableItem: mainList }
}@
-
According to http://doc.qt.nokia.com/qt-mobility-snapshot/qml-contact.html#details
you can access the phoneNumbers property of the contact, which is an array of all the phone number elements (http://doc.qt.nokia.com/qt-mobility-snapshot/qml-phonenumber.html). From here, I think you can iterate over all numbers, to find which number has a subtype of PhoneNumber.Mobile -
Thanks for the reply.
But how can i iterate over this list in QML? I thought QML didn't work this way. The only thing i saw were conditional assignments.
In good old C++, i would define a functor object that finds me the right number, but in QML i have no idea whats the best approach.
-
QML can use good old Javascript. You can create a javascript function that does that, and then call it as your variable assignment.
@function findMobilePhoneNumber(phoneNumbers) {
for ( var i=phoneNumbers.length-1; i>=0; --i ){
// Check for phoneNumber subtype, exit loop when finding the mobile one
}
}@ -
Hehe, good old Javascript. I tried that but it appears that the phoneNumbers haven't subtypes at all.
This:
@
...
ListItemText {
id: numberItem
mode: listItem.mode
role: "SubTitle"
text: Helper.findMobilePhoneNumber(phoneNumbers) //<- have changed that
}
...function findMobilePhoneNumber(phoneNumbers) {
console.log("phonenumbers length " + phoneNumbers.length );
for ( var i=phoneNumbers.length-1; i>=0; --i ){
console.log("subtypes length " + phoneNumbers[i].subTypes.length );
}
}@leads to:
@phonenumbers length 2
subtypes length 0
subtypes length 0phonenumbers length 1
subtypes length 0...@
Did I miss something?
-
That looks about right, not sure why the subTypes would be empty. Maybe try the same without your DetailFilter (just in case it might be doing strange things). Hopefully someone who's used this before can help you out.
-
Hi,
maybe this is relevant to new users on iOS & Android as well.
You can query and display the mobile numbers from a contact with this code using V-Play:import VPlayApps 1.0 App { AppListView { anchors.fill: parent model: nativeUtils.getContacts() delegate: SimpleRow { text: modelData.name detailText: modelData.phoneNumber } } }
You can find more details about it here:
https://v-play.net/updates/release-2-13-0-free-rewarded-videos-native-ads-for-google-admob-qt#get-contact-list-ios-androidCheers, Chris