Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. run cmd in qt
Qt 6.11 is out! See what's new in the release blog

run cmd in qt

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 2 Posters 383 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    azys233
    wrote on last edited by
    #1
    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.

    C 1 Reply Last reply
    0
    • A azys233
      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.

      C Offline
      C Offline
      ChrisW67
      wrote on last edited by ChrisW67
      #2

      "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.

      1 Reply Last reply
      2
      • A azys233 has marked this topic as solved on

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved