Part 7 - Additional Features¶
Describes how to export data in VCard format.
This part covers some additional features that make the address book more convenient for the frequent user.
Although our address book is useful in isolation, it would be better if we could exchange contact data with other applications. The vCard format is a popular file format that can be used for this purpose. Here we extend our address book client to allow contacts to be exported to vCard
.vcf
files.
Defining the AddressBook Class¶
We add a
QPushButton
object,exportButton
, and a corresponding public slot,exportAsVCard()
to ourAddressBook
class in theaddressbook.h
file.<Code snippet "tutorials/addressbook/part7/addressbook.h:exportAsVCard() declaration" not found> ... QPushButton *exportButton;
Implementing the AddressBook Class¶
Within the
AddressBook
constructor, we connectexportButton
‘sclicked()
signal toexportAsVCard()
. We also add this button to ourbuttonLayout1
, the layout responsible for our panel of buttons on the right.In our
exportAsVCard()
function, we start by extracting the contact’s name intoname
. We declarefirstName
,lastName
andnameList
. Next, we look for the index of the first white space inname
. If there is a white space, we split the contact’s name intofirstName
andlastName
. Then, we replace the space with an underscore (”_”). Alternately, if there is no white space, we assume that the contact only has a first name.void AddressBook::exportAsVCard() { QString name = nameLine->text(); QString address = addressText->toPlainText(); QString firstName; QString lastName; QStringList nameList; int index = name.indexOf(" "); if (index != -1) { nameList = name.split(QRegularExpression("\\s+"), Qt::SkipEmptyParts); firstName = nameList.first(); lastName = nameList.last(); } else { firstName = name; lastName = ""; } QString fileName = QFileDialog::getSaveFileName(this, tr("Export Contact"), "", tr("vCard Files (*.vcf);;All Files (*)")); if (fileName.isEmpty()) return; QFile file(fileName);As with the
saveToFile()
function, we open a file dialog to let the user choose a location for the file. Using the file name chosen, we create an instance ofQFile
to write to.We attempt to open the file in
WriteOnly
mode. If this process fails, we display aQMessageBox
to inform the user about the problem and return. Otherwise, we pass the file as a parameter to aQTextStream
object,out
. LikeQDataStream
, theQTextStream
class provides functionality to read and write plain text to files. As a result, the.vcf
file generated can be opened for editing in a text editor.if (!file.open(QIODevice::WriteOnly)) { QMessageBox::information(this, tr("Unable to open file"), file.errorString()); return; } QTextStream out(&file);We then write out a vCard file with the
BEGIN:VCARD
tag, followed by theVERSION:2.1
tag. The contact’s name is written with theN:
tag. For theFN:
tag, which fills in the “File as” property of a vCard, we have to check whether the contact has a last name or not. If the contact does, we use the details innameList
to fill it. Otherwise, we writefirstName
only.out << "BEGIN:VCARD" << '\n'; out << "VERSION:2.1" << '\n'; out << "N:" << lastName << ';' << firstName << '\n'; if (!nameList.isEmpty()) out << "FN:" << nameList.join(' ') << '\n'; else out << "FN:" << firstName << '\n';We proceed to write the contact’s address. The semicolons in the address are escaped with “\”, the newlines are replaced with semicolons, and the commas are replaced with spaces. Lastly, we write the
ADR;HOME:;
tag, followed byaddress
and then theEND:VCARD
tag.address.replace(";", "\\;", Qt::CaseInsensitive); address.replace('\n', ";", Qt::CaseInsensitive); address.replace(",", " ", Qt::CaseInsensitive); out << "ADR;HOME:;" << address << '\n'; out << "END:VCARD" << '\n'; QMessageBox::information(this, tr("Export Successful"), tr("\"%1\" has been exported as a vCard.").arg(name)); }In the end, a
QMessageBox
is displayed to inform the user that the vCard has been successfully exported.vCard is a trademark of the Internet Mail Consortium ** .
© 2022 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.