I'm trying to create a single instance Qt application and I'm at the point this works, but now I want to focus the already started instance when a second is started. QWidget::find(g_hWnd) should return the widget but it fails and crashes on w->show();
Any thoughts?
#pragma data_seg("Shared")
HWND g_hWnd = NULL;
#pragma data_seg()
#pragma comment(linker,"/section:Shared,rws")
int main(int argc, char *argv[])
{
if (g_hWnd)
{
QWidget* w = QWidget::find(g_hWnd);
w->show();
return 0;
}
else
{
QApplication a(argc, argv);
mainWindow w;
w.show();
g_hWnd = a.topLevelWidgets().at(0)->winId(); //or w.winId()?
return a.exec();
}
}
edit: I now see Trolltech released the QtSingleApplication class under LGPL.