gpt4 book ai didi

c++ - 通过 Qt GUI 向 CMD 传递/提供命令

转载 作者:行者123 更新时间:2023-11-28 04:47:06 24 4
gpt4 key购买 nike

我试图实现(并努力实现)的基本上是通过我的 QT 主窗口应用程序将命令传递给 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;

希望我可以使用普通的“附加到文件”代码,但它什么都不做,我什至没有收到错误 :(

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

这是

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

  2. 获取用户输入并让我的应用在我点击按钮时将其传递给 cmd。

这是我的整个ma​​inwindow.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()blocking 函数 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;
}

关于c++ - 通过 Qt GUI 向 CMD 传递/提供命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49069807/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com