PDA

View Full Version : how to start a program in the background



JustinTerpstra
30th September 2010, 20:43
I have two Windows programs; A and B. Program A is a QApplication with a GUI. Program B is a QCoreApplication without a GUI. I need A to kick off B and keep focus on A. When I use QProcess it starts B in a console window, but leaves that as the active window on the system. I am looking for ideas on this....

MattPhillips
1st October 2010, 04:39
setFocus() on the QMainWindow of A?

JustinTerpstra
12th October 2010, 00:51
I finally have a solution to this issue.

Part of the problem was QProcess appears to return too fast. It needs a second for the new process to get going before I try to put my main application back into focus.

Here is the nature of what I did.

bool status = newProcess->startDetached( command, QStringList(), path );

// qprocess appears to operate on events internally, be sure it happens
QTime startTime = QTime::currentTime();
while( startTime.msecsTo( QTime::currentTime() ) < 100 )
{
QCoreApplication::processEvents();
}

// ensure focus is returned to the main window
if( returnFocus )
{
while( !mainWindow->isActiveWindow() )
{
mainWindow->show();
mainWindow->activateWindow();
mainWindow->raise();
}
}