Home

QMfcApp Class Reference

The QMfcApp class provides merging of the MFC and Qt event loops. More...

#include <qmfcapp.h>

List of all member functions.

Public Members

Static Public Members


Detailed Description

The QMfcApp class provides merging of the MFC and Qt event loops.

QMfcApp is responsible for driving both the Qt and MFC event loop. It replaces the standard MFC event loop provided by CWinApp::Run(), and is used instead of the QApplication parent class.

To replace the MFC event loop reimplement the CWinApp::Run() function in the CWinApp subclass usually created by the MFC Application Wizard, and use either the static run() function, or an instance of QMfcApp created earlier through the static instance() function or the constructor.

The QMfcApp class also provides a static API pluginInstance() that drives the Qt event loop for Qt based DLLs loaded into an MFC or Win32 application.


Member Function Documentation

QMfcApp::QMfcApp ( CWinApp * mfcApp, int & argc, char ** argv )

Creates an instance of QMfcApp. mfcApp must point to the existing instance of CWinApp. argc and argv are passed on to the QApplication constructor.

Use the static function instance() to automatically use the command line passed to the CWinApp.

    QMfcApp *qtApp;

    BOOL MyMfcApp::InitInstance()
    {
        // standard MFC initialization

        int argc = ...
        char **argv = ...

        qtApp = new QMfcApp( this, argc, argv );

        // Qt GUI initialization
    }

    BOOL MyMfcApp::Run()
    {
        int result = qtApp->exec();
        delete qtApp;
        qtApp = 0;

        return result;
    }
    

See also instance() and run().

QMfcApp::~QMfcApp ()

Destroys the QMfcApp object, freeing all allocated resources.

QApplication * QMfcApp::instance ( CWinApp * mfcApp ) [static]

Creates an instance of QApplication, passing the command line of mfcApp to the QApplication constructor, and returns the new object. The returned object must be destroyed by the caller.

Use this static function if you want to perform additional initializations after creating the application object, or if you want to create Qt GUI elements in the InitInstance() reimplementation of CWinApp:

    BOOL MyMfcApp::InitInstance()
    {
        // standard MFC initialization
        // ...

        // This sets the global qApp pointer
        QMfcApp::instance( this );

        // Qt GUI initialization
    }

    BOOL MyMfcApp::Run()
    {
        int result = QMfcApp::run( this );
        delete qApp;
        return result;
    }
    

See also run().

Example: step4/qtmfc.cpp.

bool QMfcApp::pluginInstance ( HANDLE plugin = 0 ) [static]

If there is no global QApplication object (i.e. qApp is null) this function creates a QApplication instance and returns true; otherwise it does nothing and returns false.

The application installs an event filter that drives the Qt event loop in a Qt based DLL plugin that is being loaded into MFC or Win32 applications. If plugin is non-null the function loads the DLL explicitly to avoid unloading from memory.

Use this static function in your plugin initialization code, e.g. in an implementation of the DllMain entry point function:

    BOOL WINAPI DllMain( HINSTANCE hInstance, DWORD dwReason, LPVOID lpvReserved )
    {
        if ( dwReason == DLL_PROCESS_ATTACH )
            QMfcApp::pluginInstance( hInstance );

        return TRUE;
    }
    

Example: qtdll/main.cpp.

int QMfcApp::run ( CWinApp * mfcApp ) [static]

Runs the event loop for both Qt and the MFC application object mfcApp, and returns the result. This function calls instance() if no QApplication object exists and deletes the object it created.

Calling this static function in a reimplementation of CWinApp::Run() is the simpliest way to use the QMfcApp class:

    int MyMfcApp::Run()
    {
        return QMfcApp::run( this );
    }
    

Since a QApplication object must exist before Qt widgets can be created you cannot use this function if you want to use Qt-based user interface elements in, for example, the InitInstance() function of CWinApp. In such cases, create an instance of QApplication explicitly using instance() or the constructor.

See also instance().

Example: step4/qtmfc.cpp.


This file is part of the Qt Solutions. Copyright © 2003-2004 Trolltech. All Rights Reserved.

Copyright © 2003-2004 TrolltechTrademarks
Qt Solutions