範例工程文件看起來像這樣:
TEMPLATE = app
TARGET = qutlook
CONFIG += qaxcontainer
TYPELIBS = $$system(dumpcpp -getfile {00062FFF-0000-0000-C000-000000000046})
isEmpty(TYPELIBS) {
message("Microsoft Outlook type library not found!")
REQUIRES += Outlook
} else {
HEADERS = addressview.h
SOURCES = addressview.cpp main.cpp
}
工程文件使用
dumpcpp
工具將 MS Outlook 類型庫添加到工程。若這失敗,那麼生成的 makefile 將僅僅打印錯誤消息,否則構建步驟現在將運行
dumpcpp
工具在類型庫,並生成頭和 cpp 文件 (在這種情況下,
msoutl.h
and
msoutl.cpp
) 聲明並實現易於使用的 Outlook 對象 API。
class AddressView : public QWidget { Q_OBJECT public: AddressView(QWidget *parent = 0); protected slots: void addEntry(); void changeEntry(); void itemSelected(const QModelIndex &index); void updateOutlook(); protected: AddressBookModel *model; QTreeView *treeView; QPushButton *add, *change; QLineEdit *iFirstName, *iLastName, *iAddress, *iEMail; };
AddressView 類是
QWidget
子類對於用戶界麵。
QTreeView
小部件將顯示 Outlook 聯絡文件夾的內容如提供通過
model
.
#include "addressview.h" #include "msoutl.h" #include <QtGui> class AddressBookModel : public QAbstractListModel { public: AddressBookModel(AddressView *parent); ~AddressBookModel(); int rowCount(const QModelIndex &parent = QModelIndex()) const; int columnCount(const QModelIndex &parent) const; QVariant headerData(int section, Qt::Orientation orientation, int role) const; QVariant data(const QModelIndex &index, int role) const; void changeItem(const QModelIndex &index, const QString &firstName, const QString &lastName, const QString &address, const QString &email); void addItem(const QString &firstName, const QString &lastName, const QString &address, const QString &email); void update(); private: Outlook::Application outlook; Outlook::Items * contactItems; mutable QHash<QModelIndex, QStringList> cache; };
AddressBookModel 類是 QAbstractListModel 子類直接與 Outlook 通信,使用 QHash 為緩存。
AddressBookModel::AddressBookModel(AddressView *parent) : QAbstractListModel(parent) { if (!outlook.isNull()) { Outlook::NameSpace session(outlook.Session()); session.Logon(); Outlook::MAPIFolder *folder = session.GetDefaultFolder(Outlook::olFolderContacts); contactItems = new Outlook::Items(folder->Items()); connect(contactItems, SIGNAL(ItemAdd(IDispatch*)), parent, SLOT(updateOutlook())); connect(contactItems, SIGNAL(ItemChange(IDispatch*)), parent, SLOT(updateOutlook())); connect(contactItems, SIGNAL(ItemRemove()), parent, SLOT(updateOutlook())); delete folder; } }
構造函數初始化 Outlook。連接 Outlook 提供的內容即將改變的各種通知信號到
updateOutlook()
槽。
AddressBookModel::~AddressBookModel() { delete contactItems; if (!outlook.isNull()) Outlook::NameSpace(outlook.Session()).Logoff(); }
析構函數注銷會話。
int AddressBookModel::rowCount(const QModelIndex &) const { return contactItems ? contactItems->Count() : 0; } int AddressBookModel::columnCount(const QModelIndex &parent) const { return 4; }
The
rowCount()
實現返迴由 Outlook 報告的條目數。
columnCount
and
headerData
被實現為在樹視圖中展示 4 列。
QVariant AddressBookModel::headerData(int section, Qt::Orientation orientation, int role) const { if (role != Qt::DisplayRole) return QVariant(); switch (section) { case 0: return tr("First Name"); case 1: return tr("Last Name"); case 2: return tr("Address"); case 3: return tr("Email"); default: break; } return QVariant(); }
The
headerData()
實現返迴硬編碼字符串。
QVariant AddressBookModel::data(const QModelIndex &index, int role) const { if (!index.isValid() || role != Qt::DisplayRole) return QVariant(); QStringList data; if (cache.contains(index)) { data = cache.value(index); } else { Outlook::ContactItem contact(contactItems->Item(index.row() + 1)); data << contact.FirstName() << contact.LastName() << contact.HomeAddress() << contact.Email1Address(); cache.insert(index, data); } if (index.column() < data.count()) return data.at(index.column()); return QVariant(); }
The
data()
實現是模型核心。若請求數據在緩存中,使用緩存值,否則從 Outlook 獲取數據。
void AddressBookModel::changeItem(const QModelIndex &index, const QString &firstName, const QString &lastName, const QString &address, const QString &email) { Outlook::ContactItem item(contactItems->Item(index.row() + 1)); item.SetFirstName(firstName); item.SetLastName(lastName); item.SetHomeAddress(address); item.SetEmail1Address(email); item.Save(); cache.take(index); }
The
changeItem()
槽被調用當用戶使用用戶界麵改變當前條目時。Outlook 項的訪問是使用 Outlook API,而修改是使用特性 setter。最後,項被保存到 Outlook,並從緩存移除它。注意,模型將不發射數據視圖改變信號,因為 Outlook 自己會發射信號。
void AddressBookModel::addItem(const QString &firstName, const QString &lastName, const QString &address, const QString &email) { Outlook::ContactItem item(outlook.CreateItem(Outlook::olContactItem)); if (!item.isNull()) { item.SetFirstName(firstName); item.SetLastName(lastName); item.SetHomeAddress(address); item.SetEmail1Address(email); item.Save(); } }
The
addItem()
槽調用 Outlook 的 CreateItem 方法創建新的聯絡項,並將新項的特性設為用戶輸入值再保存項。
void AddressBookModel::update() { cache.clear(); emit reset(); }
The
update()
槽清零緩存,並發射
reset
() 信號通知視圖數據即將改變要求重新繪製內容。
AddressView::AddressView(QWidget *parent) : QWidget(parent) { QGridLayout *mainGrid = new QGridLayout(this); QLabel *liFirstName = new QLabel("First &Name", this); liFirstName->resize(liFirstName->sizeHint()); mainGrid->addWidget(liFirstName, 0, 0); QLabel *liLastName = new QLabel("&Last Name", this); liLastName->resize(liLastName->sizeHint()); mainGrid->addWidget(liLastName, 0, 1); QLabel *liAddress = new QLabel("Add&ress", this); liAddress->resize(liAddress->sizeHint()); mainGrid->addWidget(liAddress, 0, 2); QLabel *liEMail = new QLabel("&E-Mail", this); liEMail->resize(liEMail->sizeHint()); mainGrid->addWidget(liEMail, 0, 3); add = new QPushButton("A&dd", this); add->resize(add->sizeHint()); mainGrid->addWidget(add, 0, 4); connect(add, SIGNAL(clicked()), this, SLOT(addEntry())); iFirstName = new QLineEdit(this); iFirstName->resize(iFirstName->sizeHint()); mainGrid->addWidget(iFirstName, 1, 0); liFirstName->setBuddy(iFirstName); iLastName = new QLineEdit(this); iLastName->resize(iLastName->sizeHint()); mainGrid->addWidget(iLastName, 1, 1); liLastName->setBuddy(iLastName); iAddress = new QLineEdit(this); iAddress->resize(iAddress->sizeHint()); mainGrid->addWidget(iAddress, 1, 2); liAddress->setBuddy(iAddress); iEMail = new QLineEdit(this); iEMail->resize(iEMail->sizeHint()); mainGrid->addWidget(iEMail, 1, 3); liEMail->setBuddy(iEMail); change = new QPushButton("&Change", this); change->resize(change->sizeHint()); mainGrid->addWidget(change, 1, 4); connect(change, SIGNAL(clicked()), this, SLOT(changeEntry())); treeView = new QTreeView(this); treeView->setSelectionMode(QTreeView::SingleSelection); treeView->setRootIsDecorated(false); model = new AddressBookModel(this); treeView->setModel(model); connect(treeView->selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)), this, SLOT(itemSelected(QModelIndex))); mainGrid->addWidget(treeView, 2, 0, 1, 5); } void AddressView::updateOutlook() { model->update(); } void AddressView::addEntry() { if (!iFirstName->text().isEmpty() || !iLastName->text().isEmpty() || !iAddress->text().isEmpty() || !iEMail->text().isEmpty()) { model->addItem(iFirstName->text(), iFirstName->text(), iAddress->text(), iEMail->text()); } iFirstName->setText(""); iLastName->setText(""); iAddress->setText(""); iEMail->setText(""); } void AddressView::changeEntry() { QModelIndex current = treeView->currentIndex(); if (current.isValid()) model->changeItem(current, iFirstName->text(), iLastName->text(), iAddress->text(), iEMail->text()); } void AddressView::itemSelected(const QModelIndex &index) { if (!index.isValid()) return; QAbstractItemModel *model = treeView->model(); iFirstName->setText(model->data(model->index(index.row(), 0)).toString()); iLastName->setText(model->data(model->index(index.row(), 1)).toString()); iAddress->setText(model->data(model->index(index.row(), 2)).toString()); iEMail->setText(model->data(model->index(index.row(), 3)).toString()); }
文件的其餘部分僅使用 Qt API 實現用戶界麵,即:不直接與 Outlook 進行通信。
#include "addressview.h" #include <QApplication> int main(int argc, char ** argv) { QApplication a(argc, argv); AddressView view; view.setWindowTitle("Qt Example - Looking at Outlook"); view.show(); return a.exec(); }
The
main()
入口點函數最後實例化用戶界麵並進入事件循環。
要構建範例必須先構建
QAxContainer
庫。然後運行 make 工具在
examples/activeqt/qutlook
和運行結果
qutlook.exe
.
文件: