1,效果
2,主要代码
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <iostream>
#include <stdio.h>
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::executeCMD(const char *cmd, char *result)
{
char buf_ps[1024];
char ps[1024]={0};
FILE *ptr;
strcpy(ps, cmd);
if((ptr=popen(ps, "r"))!=NULL)
{
while(fgets(buf_ps, 1024, ptr)!=NULL)
{
strcat(result, buf_ps);
if(strlen(result)>1024)
break;
}
printf("result = %s \n", result);
pclose(ptr);
ptr = NULL;
}
else
{
printf("popen %s error\n", ps);
}
}
void MainWindow::on_pushButton_clicked()
{
char result[1024]={0};
QString strCmd = ui->lineEdit->text();
if(!strCmd.isEmpty())
{
executeCMD(strCmd.toStdString().c_str(),result);
ui->textEdit->setText(QString::fromStdString(result));
}
}
3,源码下载
http://pan.baidu.com/s/1boNlywB