QProcess::start doesn't display a console window for console programs (Windows)
I've had the same idea, trying cmd /C right now. Still having lots of problems. cmd.exe itself does launch, but it seems to ignore the command I want it to execute...
Maybe this one:
It's python but it's easy to convert it to pure c++.
btw. Have you tried QProcess::startDetached("cmd.exe"); ?
I haven't because I needed to use output redirection (setStandardOutputFile), but now that you've asked I realized that with "cmd /C start" strategy I can use that. But on the other hand, with that strategy I actually shouldn't use startDetached because I'll get two windows: one from cmd and one from the actual command.
On a second thought, with "cmd /C" and startDetached I won't need to add "start". That's interesting, trying it out right now.
QProcess is intended to run a "slave" process, so it won't have a console. Would look rather strange, if run a (GUI) program and suddenly all kinds of console windows pop up, right? Instead, the console is hidden and the STDOUT and STDERR streams are redirected by QProcess, so your "main" application can read them. If the child process had a console window, it would be empty anyway, because of the STDOUT/STDERR redirection.
Now you say that that you do need output redirection. So QProcess should do exactly what you want. Also, with output redirection enabled, what exactly would you have expected to appear in the child's console window?
BTW: If your "main" application is a console application and thus has its own console, you could still read the child process' STDOUT and/or STDERR from the QProcess object and then write that to your own console window.
I see your point. I already figured that bare QProcess won't do what I need, so I'm experimenting with QProcess::startDetached("cmd.exe", QStringList() << "/C" << command, workingDir) strategy.
So what is it that you want exactly? I still don't quite get it...
Why not something like:
connect(&m_process, SIGNAL(readyReadStandardOutput()), this, SLOT(processOutput()));
m_process.start( ... );@
QByteArray line = m_process.readLine();
printf("[childproc] %s\n", line.constData());
I want to have a command line in my program that would act exactly as if the commands are executed in a shell, with shell itself being launched implicitly. so if I want to launch any program I just type its name, press Enter and its window should appear, no matter console or GUI. But I should also be able to redirect output to a file with > symbol.
That sounds like you are trying to workaround all the "services" QProcess offers and you just want the plain "system()":http://www.cplusplus.com/reference/cstdlib/system/ command...
std::system would be good because I don't have to guess what shell the OS uses or which flags I need to execute my commands, but I can't set a working dir. That's crucial.
system() probably uses the working dir establish by chdir().
But if that doesn't work, you could also simply use a command like:
@"pushd c:\my_working_dir && do_something"@
The system() string is passed to the shell, so all shell commands work.
Anyway, if you decide to run "cmd.exe" manually, you should be using:
Any specific reason you've suggested pushd and not cd?
std::system doesn't cut it for me because it blocks until the shell exits, which is unacceptable. And I don't see a way to pass non-ASCII paths to it. So I'll have to use QProcess::startDetached after all.
[quote author="Violet Giraffe" date="1399141297"]Any specific reason you've suggested pushd and not cd?[/quote]
Because "cd" only changes the path, not the drive. At least on Windows.
At the same time "push" changes the path and drive, if required.
[quote author="Violet Giraffe" date="1399148562"]std::system doesn't cut it for me because it blocks until the shell exits, which is unacceptable. And I don't see a way to pass non-ASCII paths to it. So I'll have to use QProcess::startDetached after all.[/quote]
You could invoke it in a separate thread, so it doesn't block. And if you need Unicode support, just use _wsystem() on Windows with an UTF16 string. On Linux you can use the standard system() with an UTF8 string.
Thanks! I've googled for wide-string analog of system(), but couldn't find it.