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

[Previous: Home] [Home] [Next: Chapter 2]

Getting Started

Starting with QMotif and QApplication

To be able to use Qt, we need to create a QApplication object. The QApplication class controls all the event delivery and display management for all other Qt objects and widgets. We need to use the QMotif class from the Qt Motif Extension to allow QApplication and the XtAppContext to coexist.

The QApplication object must be created in the main() function. We will need to modify todo.c to compile with a C++ compiler, so we rename todo.c to todo.cpp.

Next, we add the appropriate includes for the QMotif and QApplication classes.

    // Qt includes
    #include <qapplication.h>
    #include <qmotif.h>

Next, we create the QMotif and QApplication objects. We create QMotif with a foreign XtAppContext, and we create QApplication with a foreign Display.

      shell         = XtVaAppInitialize(&context, APP_CLASS,
                                        optionDesc, XtNumber(optionDesc),
                                        &argc, argv,
                                        fallback_resources, 0);

      XtGetApplicationResources(shell, (XtPointer) &options, resources,
                                XtNumber(resources), (Arg *) NULL, 0);

      QMotif integrator( APP_CLASS, context );
      QApplication app( XtDisplay(shell), argc, argv );

The next change is not yet necessary, but it is included to show that the Qt Motif Extension provides a complete integration. Normally, a Motif based program would use the XtAppMainLoop() function to run the application's event loop. This is still possible, but since we are migrating to the Qt toolkit, we prefer to use the QApplication::exec() function for running the event loop.

      /*
        QMotif provides an completely integrated eventloop.  We can use
        either XtAppMainLoop() or app.exec(), but we choose app.exec() for
        demonstration purposes.
      */

      // XtAppMainLoop(context);

      return app.exec();
    }

Since we renamed todo.c to todo.cpp, we must change the project file and rerun qmake to regenerate our Makefile. When we build our project, there are compile and link errors: we will fix these in the following section.

Migrating to C++

We need to convert the code in this file to proper C++ code. Fortunately, the changes are not too large. Most files included from existing C projects are not C++ compatible, so we make them compatible by wrapping them in an extern "C" block.

    /* Demo include files */
    extern "C" {
    #include <Xmd/Help.h>
    #include <Xmd/Menus.h>
    #include <Xmd/Print.h>

    #include "page.h"
    } // extern "C"

Global C functions that are forward declared must also be wrapped into an extern "C" block.

    extern "C" {

        void manageCB(Widget, Widget, XtPointer);
        void PresentFDialog(Widget, XtPointer, XmPushButtonCallbackStruct*);
        void New(Widget, char*, XmPushButtonCallbackStruct *);
        void Save(Widget, char*, XmFileSelectionBoxCallbackStruct *);
        void Open(Widget, char*, XmFileSelectionBoxCallbackStruct *);
        void Print(Widget, char*, XmdPrintCallbackStruct *);
        void SaveIt(Widget, char*, XmPushButtonCallbackStruct *);
        void PageChange(Widget, XtPointer, XmNotebookCallbackStruct *);
        void NewPage(Widget, XtPointer, XmPushButtonCallbackStruct *);
        void DeletePage(Widget, XtPointer, XmPushButtonCallbackStruct *);
        void EditPage(Widget, XtPointer, XmPushButtonCallbackStruct *);
        void SetPage(int);
        void help_cb(Widget, XtPointer, XmAnyCallbackStruct *);

        extern void ReadDB(char*);
        extern void SaveDB(char*);

        extern Page pages[];

        Widget shell, notebook, textw, labelw;
        Widget help_widget, file_dialog;
        int currentPage = 1;
        int modified;
        extern int maxpages;
        struct passwd *user;

    } // extern "C"

The manageCB() function needs to be converted to proper C++.

    // void manageCB( widget, w_to_manage, callback_data)
    //      Widget widget;
    //      Widget w_to_manage;
    //      XtPointer callback_data;
    void manageCB( Widget widget, Widget w_to_manage, XtPointer callback_data)
    {
      if (w_to_manage != (Widget) NULL)
        XtManageChild(w_to_manage);
    }

And we need to fix two invalid casts. One is in the Save() function.

    void
    Save(Widget w, char *i, XmFileSelectionBoxCallbackStruct *i2)
    {
      // XmStringUnparse returns an XtPointer, which is typedef'ed to void*
      // So, we need to cast the XtPointer to a char*
      // if ((str = XmStringUnparse(i2->value, NULL, XmCHARSET_TEXT,
      //                            XmCHARSET_TEXT, NULL, 0, XmOUTPUT_ALL))) {
      char *str = (char*)XmStringUnparse(i2->value, 0, XmCHARSET_TEXT,
                                         XmCHARSET_TEXT, 0, 0, XmOUTPUT_ALL);
      if (str) {
        SaveDB(str);

The other invalid cast is in the Open() function.

    void
    Open(Widget w, char *i, XmFileSelectionBoxCallbackStruct *i2)
    {
      // The same cast as above in Save()
      // if ((str = XmStringUnparse(i2->value, NULL, XmCHARSET_TEXT,
      //                            XmCHARSET_TEXT, NULL, 0, XmOUTPUT_ALL))) {
      char *str = (char*)XmStringUnparse(i2->value, 0, XmCHARSET_TEXT,
                                         XmCHARSET_TEXT, 0, 0, XmOUTPUT_ALL);
      if (str) {
        ReadDB(str);

After these changes, the project compiles and links, and the application runs and operates correctly.

[Previous: Home] [Home] [Next: Chapter 2]


Copyright © 2004 Trolltech Trademarks
Qt 4.0.0-b1