How to execute a jit python console using QProcess on win10 ?
-
I'm trying to develop a custom GUI command console using QProcess, the main connection code is like this:
connect(&process, SIGNAL(readyReadStandardOutput()), this, SLOT(PrintStandardOutput())); connect(&process, SIGNAL(readyReadStandardError()), this, SLOT(PrintStandardError()));
I used
QProcess::start()
to start a new process andQProcess::write()
to write my subsequent instructions. Now this program can execute most commands likecmd
anddir
correctly in this way:// dir some path process.start("cmd"); process.waitForStarted(); process.write("dir C:\Users\John\n"); // ping a website process.kill(); process.start("ping www.google.com"); // It also can run an EXISTED python script: process.start("python C:\Users\John\test.py");
And I can see the execution result in my widget due to the connections above.
But when I tried to execute a python console (not running an existed python script) using
process.start("C:\Users\John\Anaconda3\python.exe");
or
process.start("cmd"); process.waitForStarted(); process.write("C:\Users\John\Anaconda3\python.exe");
I can neither see the python interface (like the input symbol ">>>" ) shown in my GUI. What's the different between python command console and win10 cmd? How can I execute a just in time python console using QProcess or Qt? Thanks in advance.
-
Hi
I found this on the net to that allows to have it interactive.
https://stackoverflow.com/questions/48518442/qt-start-process-in-interactive-shell/48518689Not 100% sure its what you are looking for but it will open a working python shell.
#include <QProcess> #include <QString> #include <QStringList> #include "Windows.h" class QDetachableProcess : public QProcess { public: QDetachableProcess(QObject *parent = 0) : QProcess(parent) { } void detach() { waitForStarted(); setProcessState(QProcess::NotRunning); } }; int main(int argc, char *argv[]) { QDetachableProcess process; QString program = "cmd.exe"; QStringList arguments = QStringList() << "/K" << "python.exe";; process.setCreateProcessArgumentsModifier( [](QProcess::CreateProcessArguments *args) { args->flags |= CREATE_NEW_CONSOLE; args->startupInfo->dwFlags &=~ STARTF_USESTDHANDLES; }); process.start(program, arguments); process.detach(); return 0; }
-
@mrjj
Thanks for your reply. But actually I want the standard output of python console shown in my GUI instead of recreating a new window. I'm wondering that both cmd and python are interactive consoles, why the cmd interactive interface can be shown in my GUI, but not python?
-
@Mactarvish said in How to execute a jit python console using QProcess on win10 ?:
process.start("C:\Users\John\Anaconda3\python.exe");
Do you really do it like this? You need to escape back-slashes:
process.start("C:\\Users\\John\\Anaconda3\\python.exe");
-
@jsulm Hi, as you can see in the image above, I actually used a QLineEdit to type my command, and
"C:\\Users\\John\\Anaconda3\\python.exe"
or
"C:\Users\John\Anaconda3\python.exe"
or just
"python"
can find the correct path of python.exe, but the problem still exists. -
@Mactarvish
There are many possibilities as to why this may or may not be possible, depending on howpython.exe
behaves with input/output redirection.I would start with: set your streams to be unbuffered, because there is a chance that output from
python
is still at the other end, not reaching your application. Easiest is to launch aspython -u
. (Do not useCMD
.)Secondly/independently, ignore output for now. Verify whether your host can use a
write()
to send it a python command which it acts upon successfully (e.g. create a file, or something you can test like that).