PDA

View Full Version : QProcess::startDetached fails on Mac



sgmurphy19
19th September 2008, 22:07
Use the following code to launch and external program that needs to continue running after the app that launches it closes:
QString app = "myApp";
QProcess::startDetached(app);

This works fine in windows but it fails on the Mac. Why? The app attempting the launch doens't hang, it just returns a false for success/failure.

I've tried adding the .app after the name, I've even tried setting the working directory to both the apps dir and the myApp.app/Contents/MacOS directory. Still nothing.

Any help is appreciated!

[Added by Edit]
I can successfully launch myApp by the following:
QProcess::startDetached("/usr/bin/open", QStringList() << "path_to_myApp/myApp.app");

Does it have to be like this?
Thanks

patrik08
20th September 2008, 09:26
The applications path is different on all OS!

This is a way to find installed MiniScribus http://code.google.com/p/fop-miniscribus/

first get path from appication executable and after run its...





/* find cpl MiniScribus path or exe
*/
static inline QString getFOPDefaultExeName()
{
QString exename;
QString foppath;
#if defined Q_WS_WIN
QFileInfo Pinfo;
// Try to locate Miniscribus thanks to the qsetting
QSettings softs("HKEY_LOCAL_MACHINE\\Software",QSettings::NativeFormat);
QStringList allsoftware = softs.childGroups();
QStringList ppkonly = allsoftware.filter(QRegExp("1-PPK-Schweiz"));
for (int i = 0; i < ppkonly.size(); ++i) {
const QString RealName = ppkonly.at(i); /* realpath */
///////////qDebug() << "### soft " << RealName;
if (softs.value(RealName+"/FOP MiniScribus/path").toString().size() > 6 ) {
foppath = softs.value(RealName+"/FOP MiniScribus/path").toString();
foppath.append(softs.value(RealName+"/FOP MiniScribus/Bundlename").toString()+".exe");
return foppath;
}


}

/* win not having GPL Ghostscript ! */
foppath = "";
return foppath;
#endif
#if defined Q_WS_MAC
foppath = "/Applications/MiniScribus.app/Contents/MacOS/MiniScribus";
#endif
#if defined Q_WS_X11
foppath = getGSLinuxPath(QString("MiniScribus") );
#endif
return foppath;
/* forum http://www.qtcentre.org/forum/f-qt-programming-2/t-qsettings-read-only-avaiable-10254.html */
}

extern inline QString getGSLinuxPath( QString apps = QString("gs") )
{
QStringList potential_paths;
potential_paths.append("/usr/local/bin");
potential_paths.append("/sw/bin"); /* to use on mac as same */
potential_paths.append("/opt/bin");
QProcess *process = new QProcess(NULL);
process->setReadChannelMode(QProcess::MergedChannels);
QStringList env = process->systemEnvironment();
env.replaceInStrings(QRegExp("^PATH=(.*)", Qt::CaseInsensitive), "PATH=\\1;"+potential_paths.join(";"));
process->setEnvironment(env);

process->start( QString("which") , QStringList() << apps , QIODevice::ReadOnly );
if (!process->waitForFinished()) {
return QString();
} else {
QString finder = process->readAll().trimmed();
if (finder.endsWith(apps,Qt::CaseInsensitive)) {
///////////// qDebug() << "### finder " << finder;
return finder;
} else {
return QString();
}
}
}




and here run appication + params to create a pdf from template...




proc = new QProcess(this); /* */
connect(proc, SIGNAL(finished(int)),this, SLOT(RegisterPDFstream(int)));
proc->start(Generate_Command());

/* if finished get pdf stream and save on db mysql */



QString Invoice::Generate_Command()
{

if (Current_Faktura.barcode_value.size() < 1) {
QMessageBox::information(0, tr("File Error"),tr("Unable to read barcode nummer!"));
return QString();
}
Result_fop_file_final = QString("%1/%2_result_code.fop")
.arg(FAKTURA_CACHEDIR)
.arg(Current_Faktura.barcode_value);

QFile f( Result_fop_file_final ); /* old pdf file */
if (f.exists()) {
f.remove();
}

registro->FillOnFile(FopWorkingActual.absoluteFilePath(),tex tEdit->toPlainText(),0,Result_fop_file_final);
QFile f2( PRINTONFILEFAKE ); /* old pdf file */
if (f2.exists()) {
f2.remove();
}


QStringList cmd;
#if defined Q_WS_WIN
cmd.append("\""+FopAppsFullPath+"\"");
cmd.append("-pdf");
cmd.append("\""+PRINTONFILEFAKE+"\"");
cmd.append("\""+Result_fop_file_final+"\"");
#else
cmd.append(FopAppsFullPath);
cmd.append("-pdf");
cmd.append(PRINTONFILEFAKE);
cmd.append(Result_fop_file_final);
#endif
QString makepnggreycommand = cmd.join(" ");
return makepnggreycommand;
}