逆枫゛

Qt学习群:1149411109 群文件提供博客源码,定期答疑、更新学习资料。

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

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

 

 

posted on 2016-12-07 14:15  逆枫゛  阅读(251)  评论(0)    收藏  举报