Passing/giving commands to CMD through Qt GUI

2019-08-24 19:08发布

What I'm trying to achieve (And struggling with) is basically passing commands to CMD through my QT Mainwindow application.

I would like to have my code, first run CMD (preferably hidden). I used here QProcess like this:

(in my Mainwindow.cpp file)

QString exePath = "C:/Windows/System32/cmd.exe";
      QProcess pro;
               pro.startDetached(exePath);
               pro.waitForStarted();

this question helped me a lot

However, what this answer/question lacks is actual help on "appending" commands to CMD (not sure if this is a correct term, please correct me if I'm wrong!) I have tried the following with this piece of code

(also in my Mainwindow.cpp file)

string exepathstring = "C:/Windows/System32/cmd.exe"; //because fstream doesnt accept argument of type QString
      QProcess pro;
           pro.startDetached(exePath);   //do I have to use "detached" here?
           pro.waitForFinished();   //not sure if i should use "for 
                              //finished" or "for started" or something else

      string connecttoserver = ui->lineEdit_command->text().toStdString();   //this is where people input a cmd command
                                                                             //need to convert it to to be able to append it
       fstream myoutfile(exepathstring, ios::in | ios::out | ios::app);
         myoutfile <<""<< connecttoserver << endl;

Hoping that I can use a normal "append to file" code but it does nothing, I don't even get an error :(

could someone tell me where i'm going wrong? and how can I go about achieving what I want?

which is

  1. starting cmd (preferably in hidden) upon launch of my "mainwindow app"

  2. take user input and let my app pass it to cmd upon my button click.

Here's my entire mainwindow.cpp source file

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QProcess>
#include <QString>
#include <fstream>
#include <iostream>
using namespace std;

MainWindow::MainWindow(QWidget *parent) :
  QMainWindow(parent),
      ui(new Ui::MainWindow)
         {ui->setupUi(this);}

MainWindow::~MainWindow()
{delete ui;}


void MainWindow::on_pushButton_clicked()
{
      QString exePath      = "C:/Windows/System32/cmd.exe";
      string exepathstring = "C:/Windows/System32/cmd.exe"; //because fstream doesnt accept argument of type QString

     QProcess pro;
              pro.startDetached(exePath); 
              pro.waitForFinished();   //not sure if i should use "for finished" or "for started" or something else

     string connecttoserver = ui->lineEdit_command->text().toStdString();   /*this is where people input a cmd command
                                                                           need to convert it to to be able to append it*/
     fstream myoutfile(exepathstring, ios::in | ios::out | ios::app);
             myoutfile <<""<< connecttoserver << endl;
    }

Any input would really help me a lot ^.^ also I'm really sorry if I had used wrong terminology

1条回答
Juvenile、少年°
2楼-- · 2019-08-24 19:53

If you looked at this post, one apparent problem is that you are using the static method startDetached() with the blocking function waitForFinished() ... QProcess::waitForStarted()/waitForFinished() won't catch signals from detached QProcess; Thus you could use:

QProcess pro;
pro.start(exePath); 
pro.waitForStarted(); // the correct is `waitForStarted()`

What your trying to do with fstream is not clear - to me - while in your description, you want user to send a command to your process: then this could, for instance, be :

QByteArray user_cmd = QVariant(ui->lineEdit_command->text()).toByteArray();
pro.write(user_cmd);
pro.write("\n\r"); // press Enter to execute the command

Thus your code could be:

.h

#include <QProcess>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
    Q_OBJECT
public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
private slots:
    void readResult();
    void on_pushButton_clicked();
private:
    Ui::MainWindow *ui;
     QProcess* pro;
};

.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QString>
#include <QDebug>


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    connect(ui->pushButton, &QPushButton::clicked, this, &MainWindow::on_pushButton_clicked);
    QString exePath = "C:/Windows/System32";
    pro = new QProcess(parent);
    pro->setWorkingDirectory(exePath);
    pro->setReadChannel(QProcess::StandardOutput);
    connect(pro,&QProcess::readyReadStandardOutput, this, &MainWindow::readResult);
    pro->start("cmd.exe");
    if (!pro->waitForStarted())
    {
      qDebug() << "The process didnt start" << pro->error();
    }
}
void MainWindow::on_pushButton_clicked()
{
    if (ui->lineEdit_command->text().isEmpty())
        return;
    QByteArray cmd = QVariant(ui->lineEdit_command->text()).toByteArray();
     pro->write(cmd);
     pro->write("\n\r");
     ui->lineEdit_command->clear();

}
void MainWindow::readResult()
{
    while(pro->bytesAvailable()){
    QString dirout =  pro->readLine();
    qDebug() << dirout;
    }
}
MainWindow::~MainWindow()
{
    delete ui;
}
查看更多
登录 后发表回答