我想要实现的目标(并且正在努力)基本上是通过我的QT Mainwindow应用程序将命令传递给CMD。
我想拥有我的代码,首先运行CMD(最好隐藏)。我这里使用的QProcess是这样的:
(在我的Mainwindow.cpp文件中)
QString exePath = "C:/Windows/System32/cmd.exe";
QProcess pro;
pro.startDetached(exePath);
pro.waitForStarted();
但是,这个答案/问题缺少的是对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;
希望我可以使用正常的“追加文件”代码,但它什么都不做,我甚至没有得到错误:(
谁能告诉我哪里出错了?我怎样才能实现我的目标?
是的
这是我的整个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;
}
任何输入都会对我有很大的帮助^。^如果我使用了错误的术语,我真的很抱歉
如果你看看这个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;
}