Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Get the mobile number from a contact in a list component
Forum Updated to NodeBB v4.3 + New Features

Get the mobile number from a contact in a list component

Scheduled Pinned Locked Moved QML and Qt Quick
7 Posts 3 Posters 3.7k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MrBlueSky125
    wrote on last edited by
    #1

    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: listDelegate

    ContactModel {
        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
    }
    

    }@

    1 Reply Last reply
    0
    • S Offline
      S Offline
      Slocan
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • M Offline
        M Offline
        MrBlueSky125
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • S Offline
          S Offline
          Slocan
          wrote on last edited by
          #4

          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
          }
          }@

          1 Reply Last reply
          0
          • M Offline
            M Offline
            MrBlueSky125
            wrote on last edited by
            #5

            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 0

            phonenumbers length 1
            subtypes length 0

            ...@

            Did I miss something?

            1 Reply Last reply
            0
            • S Offline
              S Offline
              Slocan
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              0
              • F Offline
                F Offline
                feldifux
                wrote on last edited by
                #7

                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-android

                Cheers, Chris

                Founder of Felgo SDK - http://felgo.com/qt

                Felgo simplifies

                • Mobile App Dev with Qt esp. iOS & Android
                • Game Development with Qt

                What others say

                Felgo scored #1 in Cross-Platform App Development Tools Report - see why: https://goo.gl/rgp3rq

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved