添加/删除/更新联系人时获取通知
Posted
技术标签:
【中文标题】添加/删除/更新联系人时获取通知【英文标题】:Get notification when contacts are added/deleted/updated 【发布时间】:2013-09-04 05:24:11 【问题描述】:在 BlackBerry 10 上,如何在添加、删除或更新联系人时通知我的 Qt 应用程序?有联系人 API 吗?
【问题讨论】:
【参考方案1】:QContactManager 有这 3 个信号:
void QContactManager::contactsAdded ( const QList<QContactLocalId> & contactIds );
void QContactManager::contactsChanged ( const QList<QContactLocalId> & contactIds );
void QContactManager::contactsRemoved ( const QList<QContactLocalId> & contactIds );
将您的插槽连接到它们以接收通知。
【讨论】:
感谢 Sashoalm 但实际上我想在我们添加、删除或更新联系人时显示弹出窗口。那么我该怎么做...请您帮我解决这个问题。 使用QMessageBox 显示一个弹出窗口。我的回答有用吗?添加/删除/更新新联系人时是否调用插槽?我没有黑莓,所以我显然无法测试。 发生了什么?任何答案对你有用吗?您是否会接受其中一个答案? 是的。你给出的关于信号的答案是有效的,但我想显示弹出窗口并且 QmessageBox 不工作。 在本网站上,通过单击答案旁边的绿色勾号来接受有用的答案被认为是礼貌的。既然我的回答有效,你能接受吗?至于弹出窗口,您为什么不在网站上单独提出一个问题——“如何在 Blackberry 10 中显示弹出窗口”?【参考方案2】:---hpp文件------
API : bb::pim::contacts
bb/pim/contacts/ContactService
bb/pim/contacts/联系方式
bb/pim/contacts/ContactAttributeBuilder
bb/pim/contacts/ContactBuilder..
见下面的例子
#include <bb/pim/contacts/ContactService>
#include <QtCore/QObject>
class ContactEditor : public QObject
Q_OBJECT
// The data properties of the contact that is created or updated
Q_PROPERTY(QString firstName READ firstName WRITE setFirstName NOTIFY firstNameChanged)
Q_PROPERTY(QString lastName READ lastName WRITE setLastName NOTIFY lastNameChanged)
Q_PROPERTY(QDateTime birthday READ birthday WRITE setBirthday NOTIFY birthdayChanged)
Q_PROPERTY(QString email READ email WRITE setEmail NOTIFY emailChanged)
// Defines whether the editor is in 'create' or 'edit' mode
Q_PROPERTY(Mode mode READ mode WRITE setMode NOTIFY modeChanged)
Q_ENUMS(Mode)
public:
/**
* Describes the mode of the contact editor.
* The mode information are used to adapt the behavior of the editor and
* provide hints to the UI.
*/
enum Mode
CreateMode,
EditMode
;
ContactEditor(bb::pim::contacts::ContactService *service, QObject *parent = 0);
void setMode(Mode mode);
Mode mode() const;
public Q_SLOTS:
/**
* Loads the contact with the given ID.
*/
void loadContact(bb::pim::contacts::ContactId contactId);
/**
* Save the currently loaded contact if in 'edit' mode or creates a new one
* if in 'create' mode.
*/
void saveContact();
/**
* Resets all fields of the contact editor.
*/
void reset();
Q_SIGNALS:
// The change notification signals of the properties
void firstNameChanged();
void lastNameChanged();
void birthdayChanged();
void emailChanged();
void modeChanged();
private:
// The accessor methods of the properties
void setFirstName(const QString &firstName);
QString firstName() const;
void setLastName(const QString &lastName);
QString lastName() const;
void setBirthday(const QDateTime &birthday);
QDateTime birthday() const;
void setEmail(const QString &email);
QString email() const;
// The central object to access the contact service
bb::pim::contacts::ContactService *m_contactService;
// The ID of the currently loaded contact (if in 'edit' mode)
bb::pim::contacts::ContactId m_contactId;
// The property values
QString m_firstName;
QString m_lastName;
QDateTime m_birthday;
QString m_email;
Mode m_mode;
;
//! [0]
#endif
-----cpp文件-----
using namespace bb::pim::contacts;
//! [0]
/**
* A helper method to update a single attribute on a Contact object.
* It first deletes the old attribute (if it exists) and adds the attribute with the
* new value afterwards.
*/
template <typename T>
static void updateContactAttribute(ContactBuilder &builder, const Contact &contact,
AttributeKind::Type kind, AttributeSubKind::Type subKind,
const T &value)
// Delete previous instance of the attribute
QList<ContactAttribute> attributes = contact.filteredAttributes(kind);
foreach (const ContactAttribute &attribute, attributes)
if (attribute.subKind() == subKind)
builder.deleteAttribute(attribute);
// Add new instance of the attribute with new value
builder.addAttribute(ContactAttributeBuilder()
.setKind(kind)
.setSubKind(subKind)
.setValue(value));
//! [0]
//! [1]
ContactEditor::ContactEditor(ContactService *service, QObject *parent)
: QObject(parent)
, m_contactService(service)
, m_contactId(-1)
, m_birthday(QDateTime::currentDateTime())
, m_mode(CreateMode)
//! [1]
//! [2]
void ContactEditor::loadContact(ContactId contactId)
m_contactId = contactId;
// Load the contact from the persistent storage
const Contact contact = m_contactService->contactDetails(m_contactId);
// Update the properties with the data from the contact
m_firstName = contact.firstName();
m_lastName = contact.lastName();
m_birthday = QDateTime::currentDateTime();
const QList<ContactAttribute> dateAttributes = contact.filteredAttributes(AttributeKind::Date);
foreach (const ContactAttribute &dateAttribute, dateAttributes)
if (dateAttribute.subKind() == AttributeSubKind::DateBirthday)
m_birthday = dateAttribute.valueAsDateTime();
m_email.clear();
const QList<ContactAttribute> emails = contact.emails();
if (!emails.isEmpty())
m_email = emails.first().value();
// Emit the change notifications
emit firstNameChanged();
emit lastNameChanged();
emit birthdayChanged();
emit emailChanged();
//! [2]
//! [3]
void ContactEditor::saveContact()
if (m_mode == CreateMode)
// Create a builder to assemble the new contact
ContactBuilder builder;
// Set the first name
builder.addAttribute(ContactAttributeBuilder()
.setKind(AttributeKind::Name)
.setSubKind(AttributeSubKind::NameGiven)
.setValue(m_firstName));
// Set the last name
builder.addAttribute(ContactAttributeBuilder()
.setKind(AttributeKind::Name)
.setSubKind(AttributeSubKind::NameSurname)
.setValue(m_lastName));
// Set the birthday
builder.addAttribute(ContactAttributeBuilder()
.setKind(AttributeKind::Date)
.setSubKind(AttributeSubKind::DateBirthday)
.setValue(m_birthday));
// Set the email address
builder.addAttribute(ContactAttributeBuilder()
.setKind(AttributeKind::Email)
.setSubKind(AttributeSubKind::Other)
.setValue(m_email));
// Save the contact to persistent storage
m_contactService->createContact(builder, false);
else if (m_mode == EditMode)
// Load the contact from persistent storage
Contact contact = m_contactService->contactDetails(m_contactId);
if (contact.id())
// Create a builder to modify the contact
ContactBuilder builder = contact.edit();
// Update the single attributes
updateContactAttribute<QString>(builder, contact, AttributeKind::Name, AttributeSubKind::NameGiven, m_firstName);
updateContactAttribute<QString>(builder, contact, AttributeKind::Name, AttributeSubKind::NameSurname, m_lastName);
updateContactAttribute<QDateTime>(builder, contact, AttributeKind::Date, AttributeSubKind::DateBirthday, m_birthday);
updateContactAttribute<QString>(builder, contact, AttributeKind::Email, AttributeSubKind::Other, m_email);
// Save the updated contact back to persistent storage
m_contactService->updateContact(builder);
//! [3]
//! [4]
void ContactEditor::reset()
// Reset all properties
m_firstName.clear();
m_lastName.clear();
m_birthday = QDateTime::currentDateTime();
m_email.clear();
// Emit the change notifications
emit firstNameChanged();
emit lastNameChanged();
emit birthdayChanged();
emit emailChanged();
//! [4]
void ContactEditor::setFirstName(const QString &firstName)
if (m_firstName == firstName)
return;
m_firstName = firstName;
emit firstNameChanged();
QString ContactEditor::firstName() const
return m_firstName;
void ContactEditor::setLastName(const QString &lastName)
if (m_lastName == lastName)
return;
m_lastName = lastName;
emit lastNameChanged();
QString ContactEditor::lastName() const
return m_lastName;
void ContactEditor::setBirthday(const QDateTime &birthday)
if (m_birthday.date() == birthday.date())
return;
m_birthday = birthday;
emit birthdayChanged();
QDateTime ContactEditor::birthday() const
return m_birthday;
void ContactEditor::setEmail(const QString &email)
if (m_email == email)
return;
m_email = email;
emit emailChanged();
QString ContactEditor::email() const
return m_email;
void ContactEditor::setMode(Mode mode)
if (m_mode == mode)
return;
m_mode = mode;
emit modeChanged();
ContactEditor::Mode ContactEditor::mode() const
return m_mode;
【讨论】:
感谢 Rajesh,但我已经完成了这段代码,但它在我们的应用程序中添加、更新联系人,然后反映在本机联系人中。实际上我想在添加时显示对话框或其他任何内容,删除或更新本机联系人应用程序中的联系人....你对他有什么想法吗 使用警报通知..检查我的另一个答案 嘿拉杰什。我正在开发一个示例应用程序,并且我想与 CallLog 核心应用程序集成。我想在我的应用程序中获取通话记录。你能帮帮我吗?【参考方案3】:像这样使用警报 -->
alert(tr("联系人已保存"));
alert(tr("联系人添加"));
alert(tr("联系人已删除"));
参考以下示例
-----------qml--------------
按钮 水平对齐:HorizontalAlignment.Center
text: qsTr("Update")
onClicked:
_app.updateRecord(idUpdateTextField.text, firstNameUpdateTextField.text, lastNameUpdateTextField.text);
-----------------cpp 文件-------
bool App::updateRecord(const QString &customerID, const QString &firstName, const QString &lastName)
bool intConversionGood = false;
const int customerIDKey = customerID.toInt(&intConversionGood);
if (!intConversionGood)
alert(tr("You must provide valid integer key."));
return false;
QSqlDatabase database = QSqlDatabase::database();
QSqlQuery query(database);
const QString sqlCommand = "UPDATE customers "
" SET firstName = :firstName, lastName = :lastName"
" WHERE customerID = :customerID";
query.prepare(sqlCommand);
query.bindValue(":firstName", firstName);
query.bindValue(":lastName", lastName);
query.bindValue(":customerID", customerIDKey);
bool updated = false;
if (query.exec())
if (query.numRowsAffected() > 0)
alert(tr("Customer with id=%1 was updated.").arg(customerID));
updated = true;
else
alert(tr("Customer with id=%1 was not found.").arg(customerID));
【讨论】:
以上是关于添加/删除/更新联系人时获取通知的主要内容,如果未能解决你的问题,请参考以下文章