通过Qt GUI将命令传递/提供给CMD [英] Passing/giving commands to CMD through Qt GUI

查看:191
本文介绍了通过Qt GUI将命令传递/提供给CMD的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我想要我的代码,首先运行CMD(最好是隐藏的)。我在这里使用QProcess是这样的:

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

(在我的 Mainwindow.cpp 文件中)

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

this question helped me a lot

但是,这个答案/问题缺少的是实际的帮助将命令添加到CMD(不确定这是否是正确的术语,如果我错了,请纠正我!)
我已经用这段代码尝试了以下操作

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

(也在我的 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;

希望我可以使用普通的附加到文件代码,但是它什么也没做,我不甚至不会出现错误:(

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?


  1. 启动时启动cmd(最好是隐藏)我的主窗口应用程序

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

接受用户输入,并让我的应用程序在单击按钮时将其传递给cmd。

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

这是我的整个 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;
    }

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

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

推荐答案

如果您查看了此 post ,一个明显的问题是您使用的是 static 方法 startDetached() blocking 函数 waitForFinished() ... QProcess :: waitForStarted() / waitForFinished()不会捕获来自分离 QProcess的信号;
因此,您可以使用:

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()`

fstream 不清楚-对我来说-在您的描述中,您希望用户向您的进程发送 command
那么,例如,是:

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

因此您的代码可能是:


.h

.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

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

这篇关于通过Qt GUI将命令传递/提供给CMD的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆