[Solved] How to add a phone number to an existing contact with javascript ?



  • Hi,

    I wonder how I can add a phone number to an existing contact ? Are there any Code snippets available ?

    Thanks.

    Bye
    Matsimoto



  • Hi,

    I wrote this using "this example":http://doc.qt.nokia.com/qtmobility/qml-contacts.html#contact-element

    @
    Rectangle {
    id: topItem
    width: 360
    height: 640
    x: 0
    y: 0

     Contact {
         id: myContact
         Name {
             firstName:"John"
             lastName:"Gates"
         }
    
         EmailAddress {
             emailAddress:"john@example.com"
         }
         EmailAddress {
             emailAddress:"mygmailaccount@gmail.com"
         }
    
         address.street:"53 Mysteet St"
         address.locality: "My City"
         address.region: "My Region"
         address.postcode:"1111"
         address.country:"My Country"
         address.subTypes:[Address.Postal, Address.Domestic]
         address.postOfficeBox:"1111"
    
         Nickname {
             nickname:"John"
         }
    
         PhoneNumber {
             number: "1111111111"
             subTypes:[PhoneNumber.Mobile]
         }
    
         PhoneNumber {
             number: "2222222222"
             subTypes:[PhoneNumber.Fax]
         }
    
         PhoneNumber {
             number: "3333333333"
             subTypes:[PhoneNumber.Landline]
         }
    
         Component.onCompleted: {
             // create new phone number
             var newNumber = Qt.createQmlObject('import QtMobility.contacts 1.1; PhoneNumber { number: "1234567890" }',
                 myContact, "newNumber");
    
             myContact.addDetail( newNumber )
             myContact.save();
    
             for( var i = 0; i < myContact.phoneNumbers.length; ++i ) {
                 console.debug( "Phone number: " + myContact.phoneNumbers[i].number )
             }
         }
     }
    
    
     Column {
         spacing:4
    
         //access the same property with different syntaxes
         Text { text:"Name(from property name):" + myContact.name.firstName + " " + myContact.name.lastName }
         Text { text:"Name(from detail type):" + myContact.detail(ContactDetail.Name).firstName + " " + myContact.name.lastName }
         Text { text:"Name(from detail name):" + myContact.detail("Name").firstName + " " + myContact.name.lastName }
    
         Text { text:"Address:" + myContact.address.street + " " + myContact.address.locality + " " + myContact.address.region + " " + myContact.address.postcode }
    
         //If a contact contains multiple details for the same detail type, you can access them with the dynamic property names.
         Text { text:"How many email accounts?:" + myContact.emails.length }
         Text { text:"Email[0]:" + myContact.emails[0].emailAddress }
    
         Text { text:"How many phone numbers?:" + myContact.phoneNumbers.length }
         Text { text:"phone number[0]:" + myContact.phoneNumbers[0].number }
         Text { text:"phone number[1]:" + myContact.phoneNumbers[1].number }
         Text { text:"phone number[2]:" + myContact.phoneNumbers[2].number }
     }
    

    }
    @

    See Component.onCompleted . It should work everywhere you can access contancts from ContactManager.
    I hope this will be usefull for you :)



  • Hi,

    OK thanks a lot.
    Your post clarified many other things too.

    Ciao
    Matsimoto



  • Nice to hear this :)

    May be I should add this code as snippet in the Wiki.



  • Jepp, I think the main thing that a someone new to QML/Javascript/QT is that this Qt.createQmlObject command is the way to create the QML Objects needed.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.