I am spawning an external program from my QT4 app as follows:
//Extract working directory for .exe
QString fileName
= fileInfo.
fileName();
//Manually reconstructing path because fileInfo.absolutePath switches
// direction of slashes, and I'm trying to eliminate possible problems
QString sPath
= m_sLaunchFilename.
left( m_sLaunchFilename.
length() - fileName.
length() );
//Need just because it's a required argument
//Create process object to launch external .exe
//Set working directory of .exe to whatever directory it lives in
obProcess.setWorkingDirectory( sPath );
//Retrieve for debugging
sPath = obProcess.workingDirectory();
//Run it!
obProcess.startDetached(m_sLaunchFilename, obList);
qDebug() << "Launching " << m_sLaunchFilename << " in working directory " << sPath;
//Extract working directory for .exe
QFileInfo fileInfo(m_sLaunchFilename);
QString fileName = fileInfo.fileName();
//Manually reconstructing path because fileInfo.absolutePath switches
// direction of slashes, and I'm trying to eliminate possible problems
QString sPath = m_sLaunchFilename.left( m_sLaunchFilename.length() - fileName.length() );
//Need just because it's a required argument
QStringList obList;
//Create process object to launch external .exe
QProcess obProcess;
//Set working directory of .exe to whatever directory it lives in
obProcess.setWorkingDirectory( sPath );
//Retrieve for debugging
sPath = obProcess.workingDirectory();
//Run it!
obProcess.startDetached(m_sLaunchFilename, obList);
qDebug() << "Launching " << m_sLaunchFilename << " in working directory " << sPath;
To copy to clipboard, switch view to plain text mode
The output is as follows:
Launching "/code/workingdir//workingdir" in working directory "/code/workingdir//"
I have written a little app specifically to help debug this problem. The code of the app is as follows:
int main( int argc, char* argv[] ) {
qDebug
() <<
"Current path is " <<
QDir::currentPath();
FILE *fp = fopen("shawsync.ini", "r" );
if( !fp ) {
QMessageBox::information(NULL,
"Error",
"Unable to open shawsync.ini!");
printf("Unable to open shawsync.ini\n");
}
else {
QMessageBox::information(NULL,
"Success",
"Success: opened shawsync.ini!");
fclose(fp);
}
} //main
int main( int argc, char* argv[] ) {
QApplication app( argc, argv );
qDebug() << "Current path is " << QDir::currentPath();
FILE *fp = fopen("shawsync.ini", "r" );
if( !fp ) {
QMessageBox::information(NULL, "Error", "Unable to open shawsync.ini!");
printf("Unable to open shawsync.ini\n");
}
else {
QMessageBox::information(NULL, "Success", "Success: opened shawsync.ini!");
fclose(fp);
}
} //main
To copy to clipboard, switch view to plain text mode
I put a text file "shawsync.ini" in the directory where I built this program. When I manually run this program from the command line, I get the output:
Current path is "/code/workingdir"
And a messagebox denoting that shawsync.ini was found. When it is launched from my other app using QProcess, the output is:
Current path is "/code/hardupdater"
And a message box denoting that shawsync.ini was not found. I have tested this in both Windows and Linux, and neither one is working. The new process is always started in the directory where I ran the parent process (in this case, /code/hardupdater), and the setWorkingDirectory seems to be ignored.
Bookmarks