分类: 嵌入式
2014-10-15 17:52:46
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
//创建输出文本框,设置为只读属性,并且是中间部件
m_LogW = new QTextEdit();
m_LogW->setReadOnly(true);
setCentralWidget(m_LogW);
//创建目录输入框,为底部栏部件
m_InputArea = new QLineEdit();
QDockWidget *qdw = new QDockWidget("Type commands here:");
qdw->setWidget(m_InputArea);
addDockWidget(Qt::BottomDockWidgetArea, qdw);
connect(m_InputArea, SIGNAL(returnPressed()),
this, SLOT(execute()));
//创建Shell进程,读取标准输出和标准错误
m_Shell = new QProcess(this);
m_Shell->setReadChannelMode(QProcess::MergedChannels);
connect(m_Shell, SIGNAL(readyReadStandardOutput()),
this, SLOT(showOutput()));
//启动Shell
m_Shell->start("cmd", QStringList(), QIODevice::ReadWrite);
}
MainWindow::~MainWindow()
{
delete ui;
}
//向Shell传输命令
void MainWindow::execute()
{
//输入命令
QString cmdStr = m_InputArea->text() + "\n";
//清除命令输入框
m_InputArea->setText("");
//传输命令到shell
QByteArray bytes = cmdStr.toUtf8();
m_Shell->write(bytes);
}
void MainWindow::showOutput()
{
//读取标准输出的数据
QByteArray bytes = m_Shell->readAllStandardOutput();
//以"\n"为标志分割数据
QStringList lines = QString(bytes).split("\n");
//遍历数据,显示到输出文本框上
foreach (QString line, lines)
{
m_LogW->append(line);
}
}