基於qt的一個簡單的shell(能夠接收shell返回值)
阿新 • • 發佈:2019-01-29
#include <QProcess>
//方法一:直接執行shell命令
//關機
void MainWindow::Shutdown()
{
QProcess::execute("shutdown now");
}
//方法二:簡單的shell編輯器
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QDebug> #include <QProcess> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); connect(ui->lineEdit,SIGNAL(returnPressed()),this,SLOT(on_pushButton_clicked())); } MainWindow::~MainWindow() { delete ui; } void MainWindow::on_pushButton_clicked() { QProcess process; process.start(ui->lineEdit->text()); process.waitForFinished(); QByteArray output = process.readAllStandardOutput(); QString str_output = output; ui->textBrowser->setText(str_output); qDebug()<<output; }
工程下載連結:https://download.csdn.net/download/future_ai/10531733