PDA

View Full Version : Events not processed using QMfcApp::pluginInstance



nokkie
28th April 2010, 04:10
Hello, I'm trying to interface with a win32 app with a dll written using qt. All I'm trying to do for now is making a modeless QDialog display. I'm following the examples here: http://qt.nokia.com/products/appdev/add-on-products/catalog/4/Windows/qtwinmigrate

Here is the code for my DLL entry:


BOOL WINAPI DllMain(HINSTANCE hinstDLL,ULONG fdwReason,LPVOID lpvReserved)
{
static bool ownApplication = FALSE;

if ( fdwReason == DLL_PROCESS_ATTACH )
{
ownApplication = QMfcApp::pluginInstance( hInstance );

char buffer[512];
sprintf( buffer, "Creating MFC App - %d\n", ownApplication );
OutputDebugStringA( buffer );
}
if ( fdwReason == DLL_PROCESS_DETACH && ownApplication )
{
OutputDebugString( TEXT("Deleting MFC App\n") );
ownApplication = FALSE;
delete qApp;
}

return (TRUE);
}


To test that events are surely not being processed, I devised a test using this QDialog I wrote:


class TestDialog : public QDialog
{
public:
TestDialog( QWidget* pParent ) : QDialog( pParent, Qt::WindowStaysOnTopHint )
{
OutputDebugString( TEXT("Creating Test Dialog\n") );

setWindowTitle( "This is a test" );
resize( 800, 600 );
setAttribute( Qt::WA_DeleteOnClose );
setModal( false );
show();
close();
}

~TestDialog()
{
// If Events are being processed, this should be outputted
OutputDebugString( TEXT("Shutting Down Test Dialog\n") );
}
};


It is very simple, since I am setting the WA_DeleteOnClose flag, it should delete the dialog in the next event update after it has been closed. If events are being updated, the debugger should output that it is shutting down the Test Dialog. Here is the code I used to test it:



QWinWidget* pWinWidget = new QWinWidget( hParentWnd );
pWinWidget->center();

// As a test to see if events work, see if this deletes itself (it doesn't)
TestDialog* pTestDialog = new TestDialog( pWinWidget );

// if this is uncommented, it gets deleted fine (events are processed)
//qApp->exec();


The following code should work if events are being processed, however the destructor of TestDialog never gets called. When I call qApp->exec() manually though, the destructor does get called and the QDialog events all work perfectly. Being that this is a DLL into a win32 application, you can imagine how qApp->exec() takes over the message handling so this is not a good solution (for example, keyboard no longer works on the original win32 application). According to the docs, QMfcApp:: pluginInstance should be able to integrate into the message loop properly. Am I doing something wrong here?

nokkie
29th April 2010, 01:23
If anyone is interested it looks like I had a QApplication instantiated but not used, this was causing conflicts with the QApp in QMfcApp. As soon as I removed that unused QApplication everything managed all the events correctly.