通过Qt GUI向CMD传递/发出命令

时间:2018-03-02 13:09:57

标签: c++ qt cmd fstream qmainwindow

我想要实现的目标(并且正在努力)基本上是通过我的QT Mainwindow应用程序将命令传递给CMD。

我想拥有我的代码,首先运行CMD(最好隐藏)。我在这里使用的QProcess如下:

(在我的 Mainwindow.cpp 文件中)

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

this question helped me a lot

然而,这个答案/问题所缺乏的是对"追加"的实际帮助。对CMD的命令(不确定这是否是正确的术语,如果我错了,请纠正我!) 我用这段代码尝试了以下内容

(也在我的 Mainwindow.cpp 文件中)

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;

希望我可以使用普通&#34;附加到文件&#34;代码,但它没有做任何事情,我甚至没有得到错误:(

有人能告诉我哪里出错了吗? 我怎样才能实现我想要的目标呢?

  1. 启动我的&#34;主窗口应用程序&#34;

  2. 后启动cmd(最好隐藏)
  3. 接受用户输入,让我的应用在点击按钮后将其传递给cmd。

  4. 这是我的整个 mainwindow.cpp 源文件

    #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;
        }
    

    任何输入都会对我有很大的帮助^。^如果我使用了错误的术语,我真的很抱歉

1 个答案:

答案 0 :(得分:1)

如果您查看了这个post,一个明显的问题是您使用static方法startDetached()阻止函数waitForFinished() ... QProcess::waitForStarted() / waitForFinished()无法从分离的 QProcess中捕获信号; 因此你可以使用:

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

您尝试对fstream做什么并不清楚 - 在您的说明中,您希望用户向您的流程发送命令: 那么这可以是:例如:

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

因此您的代码可能是:

  

·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;
}