Home

Qt based Application Extension

This examples shows how to use the QWinWidget and QMfcApp classes to implement a Qt based user interface in a plugin DLL.

The plugin implements and exports a C function showDialog that can be called by any Windows application to display a modal Qt dialog. Before a Qt based user interface can be created a QApplication object must exist, and the calling application's event loop and the Qt event loop must run together.

    #include <qtwinmigrate/qmfcapp.h>
    #include <qtwinmigrate/qwinwidget.h>

    #include <qmessagebox.h>
    #include <windows.h>

    BOOL WINAPI DllMain( HINSTANCE hInstance, DWORD dwReason, LPVOID lpvReserved )
    {
        static bool ownApplication = FALSE;

        if ( dwReason == DLL_PROCESS_ATTACH )
            ownApplication = QMfcApp::pluginInstance( hInstance );
        if ( dwReason == DLL_PROCESS_DETACH && ownApplication )
            delete qApp;

        return TRUE;
    }
The DLL entry point function DllMain uses the QMfcApp::pluginInstance function to make sure that exactly one instance of QApplication exists in the process, and that the DLL owning that instance stays loaded in memory.

    extern "C" __declspec(dllexport) bool showDialog( HWND parent )
    {
        QWinWidget win( parent );
        win.showCentered();
        QMessageBox::about( &win, "About QtMfc", "QtMfc Version 1.0\nCopyright (C) 2003" );

        return TRUE;
    }
The C function showDialog is exported from the DLL and uses the QWinWidget class to provide proper stacking and modality between the native Win32 window and the QMessageBox.

See also Windows Migration - Win32 Examples.

Copyright © 2003-2004 TrolltechTrademarks
Qt Solutions