(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:
extern QtSingleApplication * pApp;
extern QtSingleApplication * pApp;
To copy to clipboard, switch view to plain text mode
File main.cpp:
QtSingleApplication * pApp;
int main( int argc, char * argv [] )
{
pApp = new QtSingleApplication( "UniqueName", argc, argv );
if ( pApp->sendMessage( "RUNNING?" ) ) {
return 0;
}
pApp->initialize();
pMain = new clMain();
return pApp->exec();
}
QtSingleApplication * pApp;
int main( int argc, char * argv [] )
{
pApp = new QtSingleApplication( "UniqueName", argc, argv );
if ( pApp->sendMessage( "RUNNING?" ) ) {
return 0;
}
pApp->initialize();
pMain = new clMain();
return pApp->exec();
}
To copy to clipboard, switch view to plain text mode
File clMain.h:
{
Q_OBJECT
public:
virtual ~clMain( void );
private:
void vRaiseWindowForThisInst( void );
void customEvent
( QEvent * event
);
private slots:
void vMessageReceivedFromOtherInst
( const QString & msg
);
};
class clMain : public QMainWindow
{
Q_OBJECT
public:
clMain( QWidget * parent = 0 );
virtual ~clMain( void );
private:
void vRaiseWindowForThisInst( void );
void customEvent( QEvent * event );
private slots:
void vMessageReceivedFromOtherInst( const QString & msg );
};
To copy to clipboard, switch view to plain text mode
File clMain.cpp:
{
setCentralWidget( pExitAllButton );
connect( pExitAllButton, SIGNAL( clicked() ),
this, SLOT( vExitAllPressed() ) );
connect( pApp,
SIGNAL( messageReceived
( const QString & ) ),
this,
SLOT( vMessageReceivedFromOtherInst
( const QString & ) ) );
setWindowTitle( "Original Title" );
show();
}
clMain::~clMain( void )
{
;
}
void clMain::vRaiseWindowForThisInst( void )
{
setWindowTitle( "CHANGED TITLE" );
//setFocus( Qt::OtherFocusReason );
//show();
raise();
}
Event_Main_Raise
= QEvent::User + 1};
void clMain
::customEvent( QEvent * event
) {
switch ( event->type() ) {
default :
break;
case Event_Main_Raise :
vRaiseWindowForThisInst();
break;
}
}
void clMain
::vMessageReceivedFromOtherInst( const QString & msg
) {
}
void clMain::vExitAllPressed( void )
{
close();
}
clMain::clMain( QWidget * parent )
: QMainWindow( parent )
{
QPushButton * pExitAllButton = new QPushButton( "Exit All" );
setCentralWidget( pExitAllButton );
connect( pExitAllButton, SIGNAL( clicked() ),
this, SLOT( vExitAllPressed() ) );
connect( pApp, SIGNAL( messageReceived( const QString & ) ),
this, SLOT( vMessageReceivedFromOtherInst( const QString & ) ) );
setWindowTitle( "Original Title" );
show();
}
clMain::~clMain( void )
{
;
}
void clMain::vRaiseWindowForThisInst( void )
{
setWindowTitle( "CHANGED TITLE" );
//setFocus( Qt::OtherFocusReason );
//show();
raise();
}
enum QEvent::Type {
Event_Main_Raise = QEvent::User + 1
};
void clMain::customEvent( QEvent * event )
{
switch ( event->type() ) {
default :
QMainWindow::customEvent( event );
break;
case Event_Main_Raise :
vRaiseWindowForThisInst();
break;
}
}
void clMain::vMessageReceivedFromOtherInst( const QString & msg )
{
QApplication::postEvent( this, new QEvent( Event_Main_Raise ) );
}
void clMain::vExitAllPressed( void )
{
close();
}
To copy to clipboard, switch view to plain text mode
Bookmarks