run cmd in qt
Solved
General and Desktop
-
int main(int argc, char *argv[]) { QApplication app(argc, argv); // Create a Qt window QWidget window; window.setWindowTitle("Docker Container Operations"); window.setGeometry(100, 100, 400, 200); // Create a text box to display output QTextEdit outputText(&window); outputText.setGeometry(50, 50, 300, 200); // Create a button to replicate the steps QPushButton runButton("Run Steps", &window); runButton.setGeometry(50, 300, 300, 30); // Connect the button click event QObject::connect(&runButton, &QPushButton::clicked, [&outputText]() { QProcess process; // Execute "docker exec" command process.start("docker exec -it xunxiao /bin/bash"); process.waitForFinished(); outputText.append(process.readAll()); // Execute "conda activate" command process.start("conda activate xunxiao"); process.waitForFinished(); outputText.append(process.readAll()); // Execute "cd" command process.start("cd /usr/app"); process.waitForFinished(); outputText.append(process.readAll()); // Execute "python test.py" command process.start("python test.py"); process.waitForFinished(); outputText.append(process.readAll()); // Execute "ls" command process.start("ls"); process.waitForFinished(); outputText.append(process.readAll()); }); window.show(); return app.exec(); }
It doesn't work when I press the button, but I can run the command in cmd.
-
"It doesn't work" is not a good problem description. However, a self-contained program demonstrating the problem is always appreciated.
Reread the documentation for QProcess:start(). The program to be run and its arguments are separate. Something like this:
process.start( "/path/to/docker.exe", QStringList() << "exec" << "-it" << "xunxiao" << "/bin/bash" );
Your attempt to call
CD
will not work.CD
is a Windows command shell builtin and there is no separate executable to run.Your attempt to run
ls
seems doomed to fail on Windows. -