Viadeo Twitter Google Bookmarks ! Facebook Digg del.icio.us MySpace Yahoo MyWeb Blinklist Netvouz Reddit Simpy StumbleUpon Bookmarks Windows Live Favorites 
Logo Documentation Qt ·  Page d'accueil  ·  Toutes les classes  ·  Classes principales  ·  Annotées  ·  Classes groupées  ·  Fonctions  · 

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.

    #include <qapplication.h>
    #include <qmotifwidget.h>

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

    #include <Xm/Xm.h>
    #include <Xmd/Print.h>

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.

    extern "C" {
        void New(Widget, char*, XmPushButtonCallbackStruct *);
        void Open(Widget, XtPointer, XmPushButtonCallbackStruct *);
        void Save(Widget, XtPointer, XmPushButtonCallbackStruct *);
        void Print(Widget, char*, XmdPrintCallbackStruct *);
        void ShowPrintDialog(Widget, XtPointer, XmPushButtonCallbackStruct *);
        void SaveIt(Widget, char*, XmPushButtonCallbackStruct *);
        void NewPage(Widget, XtPointer, XmPushButtonCallbackStruct *);
        void DeletePage(Widget, XtPointer, XmPushButtonCallbackStruct *);
        void EditPage(Widget, XtPointer, XmPushButtonCallbackStruct *);
    } // extern "C"

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.

    #include "mainwindow.h"

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

    #include <Xm/Xm.h>
    #include <Xm/Label.h>
    #include <Xm/Notebook.h>
    #include <Xm/Text.h>
    #include <Xmd/Print.h>
    #include "page.h"

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.

      QMotif integrator( APP_CLASS, NULL,
                         optionDesc, XtNumber(optionDesc) );
      QApplication app( argc, argv );
      MainWindow mainwindow;
      app.setMainWidget( &mainwindow );

We will now use QMotifWidget to create the XmNotebook widget.

      n = 0;
      XtSetArg(args[n], XmNcurrentPageNumber, 1); n++;
      XtSetArg(args[n], XmNlastPageNumber, 100); n++;

      QMotifWidget *center =
          new QMotifWidget( &mainwindow, xmNotebookWidgetClass,
                            args, n, "notebook" );
      mainwindow.setCentralWidget( center );
      notebook = center->motifWidget();

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

      XtAddCallback(notebook, XmNpageChangedCallback,
                    (XtCallbackProc) PageChange, NULL);

      n = 0;
      XtSetArg(args[n], XmNpageNumber, 1); n++;
      XtSetArg(args[n], XmNeditMode, XmMULTI_LINE_EDIT); n++;
      textw = XmCreateScrolledText(notebook, "text", args, n);
      XtManageChild(textw);
      XtAddCallback(textw, XmNvalueChangedCallback,
                    (XtCallbackProc) TextChanged, NULL);

      n = 0;
      XtSetArg(args[n], XmNnotebookChildType, XmSTATUS_AREA); n++;
      XtSetArg(args[n], XmNpageNumber, 1); n++;
      labelw = XmCreateLabel(notebook, "label", args, n);
      XtManageChild(labelw);

      user = getpwuid(getuid());
      for (i = 0; i < MAXPAGES; i++) {
        pages[i] = NULL;
      }

      if (options.todoFile == NULL) {
        strcpy(temppath, user -> pw_dir);
        strcat(temppath, "/.todo");
        options.todoFile = XtNewString(temppath);
      } else {
        /* Copy the string for consistency */
        options.todoFile = XtNewString(options.todoFile);
      }

      ReadDB(options.todoFile);
      SetPage(0);

      mainwindow.show();
      return app.exec();
    }

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 ]

Cette page est une traduction d'une page de la documentation de Qt, écrite par Nokia Corporation and/or its subsidiary(-ies). Les éventuels problèmes résultant d'une mauvaise traduction ne sont pas imputables à Nokia. Qt 3.2
Copyright © 2012 Developpez LLC. Tous droits réservés Developpez LLC. Aucune reproduction, même partielle, ne peut être faite de ce site et de l'ensemble de son contenu : textes, documents et images sans l'autorisation expresse de Developpez LLC. Sinon, vous encourez selon la loi jusqu'à 3 ans de prison et jusqu'à 300 000 E de dommages et intérêts. Cette page est déposée à la SACD.
Vous avez déniché une erreur ? Un bug ? Une redirection cassée ? Ou tout autre problème, quel qu'il soit ? Ou bien vous désirez participer à ce projet de traduction ? N'hésitez pas à nous contacter ou par MP !
 
 
 
 
Partenaires

Hébergement Web