0

I need call QProcess to start up xterm ,after that I should send some data to it . why those code can't run correctly?

QProcess proc;
proc.start("xterm");
proc.waitForStarted();
proc.write("ls\n");
proc.waitForFinished();
proc.waitForBytesWritten();
qDebug()<<proc.readAllStandardOutput();//output: nothing
qDebug()<<proc.errorString();//output: "write error"

thank you for your help

tangbongbong
  • 195
  • 2
  • 6
  • 1
    Most of the functions you are calling have return values. How about checking those return values and finding what goes wrong? –  Feb 24 '13 at 09:58
  • I already tried that ,anything just fine but last statement – tangbongbong Feb 25 '13 at 01:25
  • what do you mean "can't run correctly"? Does it hang? Or doesn't print anything in debug stream? I don't see a reason why xterm should end its job, so probably you code hangs on `proc.waitForFinished();` – Marek R Feb 25 '13 at 17:17

1 Answers1

2

It is not quite clear what you try to do here. Your code does not make sense: you can't send xterm 'some data' (looks like you want it to execute some command for you, here ls) to its standard input.

If you want the output of ls, simply do this:

QProcess proc;
proc.start("ls");
proc.waitForFinished();
qDebug() << proc.readAllStandardOutput();

If you want an interpreter running in the background, that you can send commands for it to execute (like a shell) you may do something like:

QProcess proc;
proc.start("/bin/sh");
proc.waitForStarted();
Q_FOREACH( QString cmd, QStringList() << "ls" << "date" << "echo test" )
{
    proc.write((cmd + "\n").toLocal8Bit());
    proc.waitForReadyRead();
    qDebug() << proc.readAllStandardOutput().split('\n');
}
proc.write("exit\n");
proc.waitForFinished();
axxel
  • 1,009
  • 7
  • 11
  • Thank you for your help ,sir. I know how to call command directly, what I really need is to call qemu in console , after qemu initialized I should type in some infomation(using QProcess?) like user and password . The real problem here is why I can't send data to xterm? sorry for my english ,English is not my mother tongue – tangbongbong Feb 25 '13 at 01:17
  • I don't know anything about qemu but maybe you should have a look at the *Getting data in/out of the emulator* section of [this](http://landley.net/aboriginal/presentation.html) doc. Apparently you can do stuff like connect the simulated serial port to quemu's stdin/stdout and then communicate with that like with the shell above. I don't see xterm anywhere in that picture, though. It might very well, that what you try to do is not possible. I suggest to change the title of your question to something more descriptive like 'How to remote control qemu from an external process?". – axxel Feb 25 '13 at 16:35
  • Thanks again ,you already help me a lot. Anyway , I still can't send data to xterm ,I will study your doc. I tried `proc.start("sh -c \"echo password | sudo -S command\" ")` . It can works for me but not the best solution . I will keep in qustion it . – tangbongbong Feb 27 '13 at 12:49
  • Your welcome. I still don't understand what you try to do. What does xterm and sudo and qemu have to do with each other? I think you should try to describe what you really try do to here (and change the question title to be more descriptive). – axxel Feb 27 '13 at 14:25