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

[Previous: Using Qt Standard Dialogs] [Home] [Next: Using Existing Dialogs with QMotifDialog]

# Using Custom QDialogs

After we have replaced the standard dialogs, we move onto the custom dialogs. This project has a single custom dialog: the Page Edit dialog.

Instead of writing the code ourselves, we use Qt Designer to design our dialog. Designing a custom dialog is beyond the scope of this document: see the Qt Designer Manual if you're unfamiliar with Qt's visual design tool.

## Replacing the Page Edit Dialog

The custom QDialog description for the Page Edit dialog is saved as `pageeditdialog.ui`. We add this file to the project file by adding the line

        FORMS += pageeditdialog.ui

to the `.pro` file, and regenerate the `Makefile`. The uic utility generates the code for our custom QDialog, which is then compiled and linked into our application. (uic is invoked automatically from makefiles generated from `.pro` files.)

We need to pass the top-level QMotifWidget as the client_data argument to the `EditPage` function, which we will use as the parent for our new `PageEditDialog`. We do this the same way as we have done for the Open and Save As dialogs in `todo.cpp`.

          // XtAddCallback(selected_menu[SELECTED_PROPERTIES], XmNactivateCallback,
          //               (XtCallbackProc) EditPage, NULL);
          XtAddCallback(selected_menu[SELECTED_PROPERTIES], XmNactivateCallback,
                        (XtCallbackProc) EditPage, (XtPointer) &toplevel );

The `EditPage()` function is implemented in `actions.cpp`. We start by adding the includes needed for the `PageEditDialog` and QLineEdit.

In the `EditPage()` function, We create the `PageEditDialog`, set the initial values of the three QLineEdit widgets with values from the current page and execute the dialog.

        void
        EditPage(Widget, XtPointer client_data,
                 XmPushButtonCallbackStruct *)
        {
          if (pages[currentPage] == NULL) return;

          QWidget *toplevel = (QWidget *) client_data;
          PageEditDialog pedlg( toplevel, "page edit dialog", true );

          if (pages[currentPage] -> label != NULL)
              pedlg.titleEdit->setText( pages[currentPage]->label );
          if (pages[currentPage] -> majorTab != NULL)
              pedlg.majorEdit->setText( pages[currentPage]->majorTab );
          if (pages[currentPage] -> minorTab != NULL)
              pedlg.minorEdit->setText( pages[currentPage]->minorTab );

          int result = pedlg.exec();

          if ( result != QDialog::Accepted )
              return;
            ...

At this point in the code, the page properties should be modified. The code to do this is in the `DoEditPage()` function. We move the contents of `DoEditPage()` to this point and remove the `DoEditPage()` function completely.

The `Page` struct defined in `page.h` stores strings in `char*` arrays. Since the PageEditDialog and the data it contains will be destroyed when we return from this function, we need to convert the unicode QString data into a QCString in the local encoding and duplicate it with `qstrdup()`.

          // pages[currentPage] -> label = XmTextFieldGetString(labelEditW);
          QString qstr = pedlg.titleEdit->text().simplifyWhiteSpace();
          pages[currentPage]->label = qstrdup( qstr.local8Bit().data() );

The same process must be done for the minorTab text:

          // temp = XmTextGetString(minorTabW);
          // temp = Trim(temp);
          qstr = pedlg.minorEdit->text().simplifyWhiteSpace();
          temp = qstrdup( qstr.local8Bit().data() );

... and for the majorTab text:

          // temp = XmTextGetString(majorTabW);
          // temp = Trim(temp);
          qstr = pedlg.majorEdit->text().simplifyWhiteSpace();
          temp = qstrdup( qstr.local8Bit().data() );

[Previous: Using Qt Standard Dialogs] [Home] [Next: Using Existing Dialogs with QMotifDialog]