4. Implementing Application Functionality
- The Central Widget
- Subclassing QTableWidget
- Loading and Saving
- Implementing the Edit Menu
- Implementing the Other Menus
- Subclassing QTableWidgetItem
In the previous two chapters, we explained how to create the Spreadsheet application's user interface. In this chapter, we will complete the program by coding its underlying functionality. Among other things, we will see how to load and save files, how to store data in memory, how to implement clipboard operations, and how to add support for spreadsheet formulas to QTableWidget.
The Central Widget
The central area of a QMainWindow can be occupied by any kind of widget. Here's an overview of the possibilities:
Use a standard Qt widget.
A standard widget such as QTableWidget or QTextEdit can be used as the central widget. In this case, the application's functionality, such as loading and saving files, must be implemented elsewhere (e.g., in a QMainWindow subclass).
Use a custom widget.
Specialized applications often need to show data in a custom widget. For example, an icon editor program would have an IconEditor widget as its central widget. Chapter 5 explains how to write custom widgets in Qt.
Use a plain QWidget with a layout manager.
Sometimes the application's central area is occupied by many widgets. This can be done by using a QWidget as the parent of all the other widgets, and using layout managers to size and position the child widgets.
Use a splitter.
Another way of using multiple widgets together is to use a QSplitter. The QSplitter arranges its child widgets horizontally or vertically, with splitter handles to give some sizing control to the user. Splitters can contain all kinds of widgets, including other splitters.
Use an MDI area.
If the application uses MDI, the central area is occupied by a QMdiArea widget, and each MDI window is a child of that widget.
Layouts, splitters, and MDI areas can be combined with standard Qt widgets or with custom widgets. Chapter 6 covers these classes in depth.
For the Spreadsheet application, a QTableWidget subclass is used as the central widget. The QTableWidget class already provides most of the spreadsheet capability we need, but it doesn't support clipboard operations and doesn't understand spreadsheet formulas such as "=A1+A2+A3". We will implement this missing functionality in the Spreadsheet class.