![]() |
Home · Overviews · Examples |
This chapter covers the file handling features of Qt that we use to write loading and saving routines for the address book application.
A QFile object represents a file on disk that can be read from and written to. QFile is a subclass of the more general QIODevice class which represents many different kinds of devices.
A QDataStream object is used to serialize binary data so that it can be stored in a QIODevice and retrieved again later. Reading from a QIODevice and writing to it is as simple as opening the stream - with the respective device as a parameter - and reading from or writing to it.Defining the AddressBook Class
We declare two public slots, saveToFile() and loadFromFile(), as well as two QPushButton objects, loadButton and saveButton.
Missing snippet: tutorials/addressbook/part6/addressbook.h.
...
Missing snippet: tutorials/addressbook/part6/addressbook.h.
Implementing the AddressBook Class
In our constructor, we instantiate loadButton and saveButton. Ideally, it would be more user-friendly to set the push buttons' labels to "Load contacts from a file" and "Save contacts to a file". However, due to the size of our other push buttons, we set the labels to Load... and Save.... Fortunately, Qt provides a simple way to set tooltips with setToolTip() and we use it in the following way for our push buttons:
Missing snippet: tutorials/addressbook/part6/addressbook.cpp.
...
Missing snippet: tutorials/addressbook/part6/addressbook.cpp.
Although it is not shown here, just like the other features we implemented, we add the push buttons to the layout panel on the right, button1Layout, and we connect the push buttons' clicked() signals to their respective slots.
For the saving feature, we first obtain fileName using QFileDialog::getSaveFileName(). This is a convenience function provided by QFileDialog, which pops up a modal file dialog and allows the user to enter a file name or select any existing .abk file. The .abk file is our Address Book extension that we create when we save contacts.
Missing snippet: tutorials/addressbook/part6/addressbook.cpp.
The file dialog that pops up is displayed in the screenshot below:
Next, we attempt to open the file in WriteOnly mode. If this is unsuccessful, we display a QMessageBox to inform the user.
Missing snippet: tutorials/addressbook/part6/addressbook.cpp.
Otherwise, we instantiate a QDataStream object, out, the read the open file. QDataStream requires that the same version of the stream is used for reading and writing. We ensure that this is the case by setting the version used to the version introduced with Qt 4.3 before serializing the data to file.
Missing snippet: tutorials/addressbook/part6/addressbook.cpp.
For the loading feature, we also obtain fileName using QFileDialog::getOpenFileName(). This function, the counterpart to QFileDialog::getSaveFileName(), also pops up the modal file dialog and allows the user to enter a file name or select any existing .abk file to load it into the address book.
Missing snippet: tutorials/addressbook/part6/addressbook.cpp.
On Windows, for example, this function pops up a native file dialog, as shown in the following screenshot.
Copyright © 2008 Trolltech | Trademarks | Qt Jambi 4.4.0_01 |