QXmlStream Bookmarks ExampleThe QXmlStream Bookmarks example provides a reader for XML Bookmark Exchange Language (XBEL) files using Qt's QXmlStreamReader class for reading, and QXmlStreamWriter class for writing the files. The XbelWriter class contains a private instance of QXmlStreamWriter, which provides an XML writer with a streaming API. XbelWriter also has a reference to the QTreeWidget instance where the bookmark hierarchy is stored. The XbelWriter constructor accepts a treeWidget to initialize within its definition. We enable QXmlStreamWriter's auto-formatting property to ensure line-breaks and indentations are added automatically to empty sections between elements, increasing readability as the data is split into several lines. The writeFile() function accepts a QIODevice object and sets it using setDevice(). This function then writes the document type definition(DTD), the start element, the version, and treeWidget's top-level items. The writeItem() function accepts a QTreeWidgetItem object and writes it to the stream, depending on its tagName, which can either be a "folder", "bookmark", or "separator". The XbelReader contains a private instance of QXmlStreamReader, the companion class to QXmlStreamWriter. XbelReader also contains a reference to the QTreeWidget that is used to group the bookmarks according to their hierarchy. The XbelReader constructor accepts a QTreeWidget to initialize the treeWidget within its definition. A QStyle object is used to set treeWidget's style property. The folderIcon is set to QIcon::Normal mode where the pixmap is only displayed when the user is not interacting with the icon. The QStyle::SP_DirClosedIcon, QStyle::SP_DirOpenIcon, and QStyle::SP_FileIcon correspond to standard pixmaps that follow the style of your GUI. The read() function accepts a QIODevice and sets it using setDevice(). The actual process of reading only takes place if the file is a valid XBEL 1.0 file. Note that the XML input needs to be well-formed to be accepted by QXmlStreamReader. Otherwise, the raiseError() function is used to display an error message. Since the XBEL reader is only concerned with reading XML elements, it makes extensive use of the readNextStartElement() convenience function. The errorString() function is used if an error occurred, in order to obtain a description of the error complete with line and column number information. The readXBEL() function reads the name of a startElement and calls the appropriate function to read it, depending on whether if its a "folder", "bookmark" or "separator". Otherwise, it calls skipCurrentElement(). The Q_ASSERT() macro is used to provide a pre-condition for the function. The readTitle() function reads the bookmark's title. The readSeparator() function creates a separator and sets its flags. The text is set to 30 "0xB7", the HEX equivalent for period. The element is then skipped using skipCurrentElement(). The MainWindow class is a subclass of QMainWindow, with a File menu and a Help menu. The MainWindow constructor instantiates the QTreeWidget object, treeWidget and sets its header with a QStringList object, labels. The constructor also invokes createActions() and createMenus() to set up the menus and their corresponding actions. The statusBar() is used to display the message "Ready" and the window's size is fixed to 480x320 pixels. The open() function enables the user to open an XBEL file using QFileDialog::getOpenFileName(). A warning message is displayed along with the fileName and errorString if the file cannot be read or if there is a parse error. The saveAs() function displays a QFileDialog, prompting the user for a fileName using QFileDialog::getSaveFileName(). Similar to the open() function, this function also displays a warning message if the file cannot be written to. The about() function displays a QMessageBox with a brief description of the example. In order to implement the open(), saveAs(), exit(), about() and aboutQt() functions, we connect them to QAction objects and add them to the fileMenu and helpMenu. The connections are as shown below: The createMenus() function creates the fileMenu and helpMenu and adds the QAction objects to them in order to create the menu shown in the screenshot below: The main() function instantiates MainWindow and invokes the show() function. See the XML Bookmark Exchange Language Resource Page for more information about XBEL files. Files: |