PDA

View Full Version : how to execute multiple commands at a time



PHANI
14th February 2012, 15:35
hi, i want to execute mutiple commands at a time when i click button. this slot is not working properly when i commented to the second write it is working fine.please some one clarify my doubt. is it possible to execute multiple commands one by one?if possible how to do it?



process = new QProcess(this);
process->start ("sh");
process->waitForStarted();
process->write("vi sample.txt\n");
process->write("vi text.text\n");
process->closeWriteChannel();
process->waitForFinished();

myta212
15th February 2012, 08:07
Hi,
I think this is a duplicate question.

Please check this link : http://toto-share.com/2011/11/qt-qprocess-in-a-loop/
I think you will get an idea how to solve your problem.
Thank you.

Best regards,

myta

Spitfire
20th February 2012, 09:48
I think you're doing it wrong. you can't simply open two vi's in a row (in a single terminal).
open a terminal and write what you're writing to the process and you'll see it doesn't work.
In your case you have to exit first vi to open another or open another shell (process).
in other cases you can use ";" as a separator of commands, ie "cd /home/my_user/;ls;touch test.file;".

PHANI
22nd February 2012, 02:49
Thanks for replying ."Spitfire" your suggestion is correct we cannot open two vi's at a time if i closed first one next vi window is opening.i got one more problem


void MainWindow::on_pushbuttonclicked()
{
process3->setWorkingDirectory ("/hdrd/iimd/");
process3->start ("gnuplot");
process3->waitForStarted();
process3->write( "set pm3d\n" );
process3->write( "splot \"plot.dat\" u 1:2:3 with line &\n" );
process3->waitForFinished();
//process3->closeWriteChannel();
}



This is my code actually this code will open some other window which is having gnuplot(graph).with out "process->closewritechannel()" i am getting ouput properly but when clicked the pushbutton again it showing some warning like this "QProcess is already running".i tried with adding "process->closewritechannel()" statement output window is just appearing one or two seconds and closeing ...give some idea how to make that output window stable and terminate the process

Spitfire
22nd February 2012, 09:01
You can't do it well in a single method because you'll freeze the UI.

waitForFinished() is a blocking call and will make your app unresponsive.
Instead, you should use finished() signal from QProcess and connect it to a slot which will terminate the process.

As to the other issue, it should be obvious why you get the message - it's because you never exit the process and try to run it again (unless you do it in other place which you didn't show).


void MainWindow::MainWindow()
{
connect( process3, SIGNAL( finished() ), this, SLOT( process3FinishedSlot() ) );
}

void MainWindow::on_pushbuttonclicked()
{
process3->setWorkingDirectory ("/hdrd/iimd/");
process3->start ("gnuplot");
process3->waitForStarted(); // blocking call, consider using started() signal if it takes long to start the process
process3->write( "set pm3d\n" );
process3->write( "splot \"plot.dat\" u 1:2:3 with line &\n" );
}

void MainWindow::process3FinishedSlot( void )
{
process3->close();
}