PDA

View Full Version : QProcess and Mac OS 10.6.7 Snow Leopard



Olivia
13th April 2011, 15:22
Hi
I have some problems with running terminal commands using QProcess. Is there any special way to do it?

Here is my code:


void MainWindow::on_actionCreate_triggered()
{
p.start("tar -zcvf " + archivename + " " + filenames);
bool success = p.waitForStarted();
ui->textEdit->append(p.readAllStandardOutput());
ui->textEdit_2->append(p.readAllStandardError());
if(success)
{
std::cout << "tar -zcvf " + archivename.toStdString() + " " +
filenames.toStdString() << std::endl;
}
p.terminate();
}


My output:


Starting /Users/olivia/....app/Contents/MacOS/wtorek...
tar -zcvf archive.tgz *.h *.cpp


Of course I have tar and in Terminal.app everything is working.
I have Qt 4.7.2.

stampede
13th April 2011, 18:22
What is the output of


qDebug() << QDir::currentDirPath();

Does it works if you change the dir before invoking QProcess:


QDir::setCurrent("/path/to/dir with/cpp and h files");

?

Olivia
13th April 2011, 19:52
currentDirPath is not working, so I used currentPath instead (QAssistants advice).

It shows "/.../wtorek-build-desktop/wtorek.app/Contents/MacOS" what is incorrect. I fixed it with:


QDir::setCurrent("../../../../wtorek");


I also edited p.start:


p.start("tar", QStringList() << " -zcvf " + archivename + " " + filenames);


Now it prints the correct command line command:

tar -zcvf archive.tgz *.h *.cpp

But it doesn't work either... I think don't think it starts process, or path to tar is wrong. I also tried /usr/bin/tar without any luck.

stampede
13th April 2011, 20:31
If you want to provide an argument list, you have to use operator << to add new files, not +. Following code works ok:


QProcess p;
p.start("C:/Program Files/7-zip/7z.exe", QStringList() << "a" << "test.zip" << "*.cpp");
p.waitForStarted();

I don't think you should call p.terminate(), maybe your process simply couldn't finish before you teminated it. Try to wait for finished as well:


void MainWindow::on_actionCreate_triggered()
{
p.start("tar -zcvf " + archivename + " " + filenames);
if( ! p.waitForStarted() ){
qDebug() << "waitForStarted failed";
return ;
}
ui->textEdit->append(p.readAllStandardOutput());
ui->textEdit_2->append(p.readAllStandardError());
if( !p.waitForFinished() ){
qDebug() << "waitForFinished failed";
}
}

Olivia
13th April 2011, 21:39
After applaying all your hints I was able to create a working version. I used:


void MainWindow::on_actionCreate_triggered()
{
//only for Mac OS X!
QDir::setCurrent("../../../../wtorek");
connect(&p, SIGNAL(readyReadStandardOutput()), this, SLOT(showOutput()));
connect(&p, SIGNAL(readyReadStandardError()), this, SLOT(showErrors()));
p.start("tar -zcvf " + archivename + " " + filenames); --> only version working, others are having troubles with f option and filename
if(!p.waitForStarted() ) {
qDebug() << "waitForStarted failed";
return ;
}
if( !p.waitForFinished() ){
qDebug() << "waitForFinished failed";
}
p.terminate(); --> I can terminate now
}
void MainWindow::showOutput() {
ui->textEdit->append(p.readAllStandardOutput());
}
void MainWindow::showErrors() {
ui->textEdit_2->append(p.readAllStandardError());
}


But there are still some problems. Why wildcards are not accepted (example *.h)? Moreover in Mac everything is written to sdterr instead of stdout.

stampede
14th April 2011, 08:44
p.start("tar -zcvf " + archivename + " " + filenames); --> only version working, others are having troubles with f option and filename
Maybe you should watch out for additional spaces, because I don't think options passed to program have any influence on QProcess internals, try without the spaces:


p.start("tar -zcvf", QStringList() << archivename << filenames.split(QRegExp("\\s"),QString::SkipEmptyParts) );


p.terminate(); --> I can terminate now
Yes, but... the process is already finished :)

Moreover in Mac everything is written to sdterr instead of stdout.
Sorry, I'm not a Mac user, can't help you with this one. But have you tried QProcess::setReadChannel (http://doc.qt.nokia.com/latest/qprocess.html#setReadChannel) ?

Olivia
15th April 2011, 21:28
Thanks for help. Additional spaces or no spaces were the problem.

In Mac OS there is something wrong with channels. App works great on Linux;).