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

Using Qt Standard Dialogs

[ Previous: Preparing to Migrate the User Interface ] [ Home ] [ Next: Using Custom QDialogs ]

We start by using two of the Qt Standard Dialogs: QFileDialog and QMessageBox. Both of these classes provide useful static convenience functions.

QFileDialog::getOpenFileName() replacement for the Open dialog
QFileDialog::getSaveFileName() replacement for the Save As dialog
QMessageBox::information() replacement for Page Delete dialog
QMessageBox::warning() replacement for IO Error dialog.

Each of these functions takes a QWidget *parent argument. If we pass zero as the parent argument, then we will have dialogs that are centered on the screen, instead of being centered over our main window. We can have QMotifWidget create our XmMainWindow, and we can then use this class as the parent for both Motif dialogs and Qt dialogs.

We need to include the appropriate headers for QMotifWidget and QFileDialog in todo.cpp.


Next, we make a few modifications to how the application is initialized. We could initialize Xt/Motif and create the XtAppContext ourselves, but QMotif can do this for us. We also let QApplication open the connection to the X server. Next, we create a QMotifWidget, passing xmMainWindowWidgetClass as the widgetclass argument. We can now use the QMotifWidget::motifWidget() function to access the Motif widget. The shell widget is created automatically by QMotifWidget. We use XtParent() to access it. The top-level window is now a QMotifWidget, which means we can use it as the parent for the Qt Standard Dialogs.


Replacing the Open and Save As Dialogs

First, we completely remove all use of the existing Motif file selection dialog. We remove the Xm/FileSB.h include, the global file_dialog variable, and the code to create the dialog. We also remove the PresentFDialog() callback function. None of this code is needed to use QFileDialog.

After removing the PresentFDialog() callback function, we need to make Open and Save As popup-menu callbacks call the Open() and Save() functions.

First we must change the declaration of these two functions.


We also change the arguments to the callbacks. We pass the top-level QMotifWidget as the client_data to these functions, since we will be using it as the parent for the QFileDialog.


    ...


Next, we modify the Save() function to use QFileDialog::getSaveFileName().


... and the Open() function to use QFileDialog::getOpenFileName().


After we build the project, the application runs and operates as expected. The difference is that the Open and Save As dialogs now use QFileDialog.

Replacing the Page Delete and IO Error Dialogs

The Page Delete dialog is created and used in actions.c. We need to migrate this file to C++. We rename it to actions.cpp, modify the project file and regenerate the Makefile.

The changes required to make actions.cpp compile are minimal. We need to wrap more C header files and global variables in an extern "C" block.


We need to forward declare the NewPage(), DeletePage(), EditPage() and SaveIt() functions so that the compiler generates the correct symbols for these functions.


We need to fix a single invalid pointer cast.


And we need to change the variable named new to newstr in the Trim() function.

We can now change the DeletePage() function to use QMessageBox::information().

First, we need to make sure we include the proper header for QMessageBox.


The code for DeletePage() looks like this:


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

Next, we change todo.cpp to pass the top-level QMotifWidget as the client_data tot he DeletePage() function.


The IO Error dialog is created and used in io.c. We need to migrate this file to C++. We rename it to io.cpp, modify the project file and regenerate the Makefile.

The changes required to make io.cpp compile are minimal. We need to wrap more C header files and global variables in an extern "C" block.


We need to forward declare the ReadDB() and SaveDB() functions so that the compiler generates the correct symbols for these functions.


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


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


We can now change the ReadDB() and SaveDB() functions to use QMessageBox::warning().

First, we need to make sure we include the proper header for QMessageBox.


The code for ReadDB() looks like this:

    ...

The code for SaveDB() looks like this:

    ...

After we build the project, the application runs and operates as expected. The difference is that the Page Delete and IO Error dialogs now use QMessageBox.

[ Previous: Preparing to Migrate the User Interface ] [ Home ] [ Next: Using Custom QDialogs ]


Copyright © 2005 TrolltechTrademarks
Qt 3.3.6