【发布时间】:2018-03-02 13:09:57
【问题描述】:
我想要实现(并且正在努力)基本上是通过我的 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;
希望我可以使用普通的“附加到文件”代码,但它什么也没做,我什至没有收到错误:(
谁能告诉我哪里出错了? 我怎样才能实现我想要的?
这是
在启动我的“主窗口应用程序”时启动 cmd(最好是隐藏的)
获取用户输入并让我的应用在单击按钮时将其传递给 cmd。
这是我的整个 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;
}
任何输入都会对我有很大帮助 ^.^ 如果我使用了错误的术语,我真的很抱歉
【问题讨论】:
-
与您的问题没有直接关系,但正确缩进您的代码将使人们更容易提供帮助。 :)
-
感谢您指出这一点,希望现在更好:D
-
我觉得现在更糟了,不知何故,哈哈。 :s 如果在 Creator 中选择代码然后按 Ctrl+I 会自动修复。
标签: c++ qt cmd fstream qmainwindow