(Qt user for 6 months, but this makes me realize I am still a newbie ...
Thank you in advance for your assistance.)

Environment:
Qt 4.1.0
Windows 2000 SP4
Microsoft VC 6.0

What is desired:
I am using the QtSingleApplication class to guarantee only one instance of an application
is running.

When a second instance is started, I want the original instance to become the application
of focus and the second instance to terminate.

What is observed:
The second instance terminates; however, the original instance does not gain
the input focus. The original instance's taskbar button usually flashes, but its
main window remains hidden behind other windows.

vRaiseWindowForThisInst() is being called because the window title is changed.

I have attempted to strip down the source files to the essentials ...

File main.h:
Qt Code:
  1. extern QtSingleApplication * pApp;
To copy to clipboard, switch view to plain text mode 

File main.cpp:
Qt Code:
  1. QtSingleApplication * pApp;
  2.  
  3. int main( int argc, char * argv [] )
  4. {
  5. pApp = new QtSingleApplication( "UniqueName", argc, argv );
  6. if ( pApp->sendMessage( "RUNNING?" ) ) {
  7. return 0;
  8. }
  9. pApp->initialize();
  10.  
  11. pMain = new clMain();
  12.  
  13. return pApp->exec();
  14. }
To copy to clipboard, switch view to plain text mode 

File clMain.h:
Qt Code:
  1. class clMain : public QMainWindow
  2. {
  3. Q_OBJECT
  4.  
  5. public:
  6. clMain( QWidget * parent = 0 );
  7. virtual ~clMain( void );
  8.  
  9. private:
  10. void vRaiseWindowForThisInst( void );
  11. void customEvent( QEvent * event );
  12.  
  13. private slots:
  14. void vMessageReceivedFromOtherInst( const QString & msg );
  15. };
To copy to clipboard, switch view to plain text mode 

File clMain.cpp:
Qt Code:
  1. clMain::clMain( QWidget * parent )
  2. : QMainWindow( parent )
  3. {
  4. QPushButton * pExitAllButton = new QPushButton( "Exit All" );
  5. setCentralWidget( pExitAllButton );
  6.  
  7. connect( pExitAllButton, SIGNAL( clicked() ),
  8. this, SLOT( vExitAllPressed() ) );
  9.  
  10. connect( pApp, SIGNAL( messageReceived( const QString & ) ),
  11. this, SLOT( vMessageReceivedFromOtherInst( const QString & ) ) );
  12.  
  13. setWindowTitle( "Original Title" );
  14. show();
  15. }
  16.  
  17. clMain::~clMain( void )
  18. {
  19. ;
  20. }
  21.  
  22. void clMain::vRaiseWindowForThisInst( void )
  23. {
  24. setWindowTitle( "CHANGED TITLE" );
  25.  
  26. //setFocus( Qt::OtherFocusReason );
  27. //show();
  28. raise();
  29. }
  30.  
  31. enum QEvent::Type {
  32. Event_Main_Raise = QEvent::User + 1
  33. };
  34.  
  35. void clMain::customEvent( QEvent * event )
  36. {
  37. switch ( event->type() ) {
  38. default :
  39. QMainWindow::customEvent( event );
  40. break;
  41.  
  42. case Event_Main_Raise :
  43. vRaiseWindowForThisInst();
  44. break;
  45. }
  46. }
  47.  
  48. void clMain::vMessageReceivedFromOtherInst( const QString & msg )
  49. {
  50. QApplication::postEvent( this, new QEvent( Event_Main_Raise ) );
  51. }
  52.  
  53. void clMain::vExitAllPressed( void )
  54. {
  55. close();
  56. }
To copy to clipboard, switch view to plain text mode