Home | All Classes | Main Classes | Annotated | Grouped Classes | Functions |

# Using Qt Main Window Classes

[ Previous: Using Existing Dialogs with QMotifDialog ] [ Home ] [ Next: Refactoring Existing Code ]

After we have replaced all the dialogs, we are ready to begin replacing the Main Window. As mentioned above, we will replace the existing `XmMainWindow` and popup-menu heirarchy with Qt Main Window Classes.

We will use Qt Designer to design our new main window.

## Implementing the Main Window

The description for the Main Window is saved as `mainwindow.ui`. We add this file to the project file and regenerate the `Makefile`. The uic utility generates the code for our Main Window, which is then compiled and linked into our application.

Qt Designer also created the `mainwindow.ui.h` file. We need to add the implementation for our Main Window to this skeleton implementation.

We begin by adding the necessary includes for QApplication and QMotifWidget.

We need includes for the Motif callback structs and the `XmdPrint` widget.

We are now ready to add implementations for the slots in our Main Window. We have one slot per menu item. Each slot will call the existing callback functions found in `todo.cpp` and `actions.cpp`.

File menu
New | `MainWindow::fileNew()` | calls the `New()` callback
Open | `MainWindow::fileOpen()` | calls the `Open()` callback
Save | `MainWindow::fileSave()` | calls the `SaveIt()` callback
Save As | `MainWindow::fileSaveAs()` | calls the `Save()` callback
Print | `MainWindow::filePrint()` | calls the `ShowPrintDialog()` callback
Exit | `MainWindow::fileExit()` | calls QApplication::quit()
Selected menu
Properties | `MainWindow::selProperties()` | calls the `EditPage()` callback
New | `MainWindow::selNewPage()` | calls the `NewPage()` callback
Delete to Trash | `MainWindow::selDeletePage()` | calls the `DeletePage()` callback

We need to add forward declarations for these callbacks before we can use them.

Each of the existing callback functions takes three arguments. We pass NULL to all of the arguments in this example (with a few exceptions, see below). The existing code does not rely on any of the arguments. Each slot implementation is a single line calling the related callback function. The code is not very interesting and would just take up space, so we've omitted it.

There are four exceptions to the above. The `Open()`, `Save()`, `EditPage()` and `DeletePage()` callbacks accept a pointer to the toplevel QWidget as argument 2 (the client_data argument). For these four functions, we pass this as the second argument, which is a toplevel `MainWindow` derived from QMainWindow.

## Replacing the Main Window

The next step is to use the new Main Window in our application. The changes needed in `todo.cpp` are large due to the large amount of code being removed.

First, we add the include for our new Main Window.

We can cleanup the Motif includes, since many of them are no longer needed.

The `QuitAppl()` and `manageCB()` callbacks are not needed any more, so we remove them. We do not need the global `shell` variable either. We remove it and all references to it in the `New()`, `Save()` and `Open()` callbacks.

In `main()`, we make the large changes. First, we use our new `MainWindow` instead of QMotifWidget with `XmMainWindow`.

We will now use QMotifWidget to create the `XmNotebook` widget.

We remove all of the code used to create the Motif menus. The remaining code in `main()` is self-explanatory.

Our application is now using QMainWindow instead of `XmMainWindow`. After we build the project, the application runs and operates as expected.

[ Previous: Using Existing Dialogs with QMotifDialog ] [ Home ] [ Next: Refactoring Existing Code ]