With all the choices available, which is the best format to use, and should we use the Python or the PyQt classes?
Using a binary format is best for performance and platform independence, and it is also the simplest to implement. Using a plain text format is appropriate for small files that typically hold only simple values like strings, numbers, and dates, and that are intended to be hand-edited. Even so, there is a risk that the user's text editor will assume a different encoding from the one we have used. We recommend using UTF-8 for all plain text formats, since it is becoming the de facto standard encoding. Reading and writing XML is a lot slower than reading and writing binary files (except for small files, that is, less than ~1MB), but it is worth offering, at least as export and import formats. After all, if our users can export and import their data in XML format, it gives them the ability to export their data, process it with some other tool, and then import back the processed data, without having to know or care about the details of the binary format our application normally uses.
As for whether we use the Python or PyQt classes, it probably does not matter at all for small files holding simple data types. If we want to minimize our programming effort, using the cPickle module is probably the easiest route. But if we have large files (multimegabytes) or if we use complex PyQt types like QBrush, QCursor, QFont, QIcon, QImage, and so on, it is easiest and most efficient to use QDataStream since it can read and write all these types directly. The one drawback of using a binary format is that if we want to change our format, we must at least change our load method so that it can load both our new and our old formats. This is not a problem in practice, as long as we include a file version at the start of our data after the magic number, since we can use this to determine which loading code to use.
At this stage we have covered the fundamentals of GUI programming. We can create main window applications, with menus, toolbars, and dock windows, and we can create and pop up any kind of dialog we like. We have also learned how to use Qt Designer to simplify and speed up dialog design, and we saw how to load and save application data in various formats. In Part III we will both deepen and broaden our GUI programming knowledge, learning how to handle multiple documents and how to create complex dialogs that are manageable for the user. We will also explore some of PyQt's major architectural features, from its low-level event-handling mechanism to the creation of custom widgets, including coverage of 2D graphics, as well as higher-level features including item-based graphics, rich text (HTML) handling, and model/view programming.
Was this article helpful?