首页 > 其他 > 详细

【Qt】使用QProcess调用其它程序或脚本

时间:2014-08-20 01:20:05      阅读:471      评论:0      收藏:0      [点我收藏+]

大概试了一下,还是不错的,不过字符编码问题还不太好解决:

代码:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>
#include <QProcess>
#include <QTextCodec>
#include <QCloseEvent>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QTextCodec::setCodecForLocale(QTextCodec::codecForName("KOI8-R"));
    ui->progressBar->setValue(0);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_clicked()
{
    process = new QProcess(this);
    QStringList str;
    str << "";

    if (ui->pushButton->text() == "stop")
    {
        process->write("quit");
        process->kill();
        return;
    }

    QString exe = QApplication::applicationDirPath().append("/test.bat");

    connect(process, SIGNAL(started()), this, SLOT(start_process()));
    connect(process, SIGNAL(readyReadStandardOutput()), this, SLOT(start_read_output()));
    connect(process, SIGNAL(readyReadStandardError()), this, SLOT(start_read_err_output()));
    connect(process, SIGNAL(finished(int,QProcess::ExitStatus)), this, SLOT(finish_process(int, QProcess::ExitStatus)));

    process->start(exe);
    if (false == process->waitForStarted())
    {
        ui->textBrowser->append("this process can not be called.");
    }

    ui->pushButton->setText("stop");
}

void MainWindow::start_process()
{
    ui->progressBar->setVisible(true);
    ui->progressBar->setValue(0);
    //QMessageBox::information(this, "ok", "ok", QMessageBox::Ok);
    ui->textBrowser->append(tr("process start"));
}

void MainWindow::finish_process(int exitCode, QProcess::ExitStatus exitStatus)
{
    if (exitStatus == QProcess::NormalExit)
    {
        ui->textBrowser->append(tr("process exit normal"));
    }
    else
    {
        ui->textBrowser->append(tr("process exit crash"));
    }
    ui->progressBar->setValue(100);
    ui->textBrowser->append("process finised");
    ui->pushButton->setText("ok");
}

void MainWindow::start_read_output()
{
    QByteArray ba = process->readAllStandardOutput();
    ui->textBrowser->append(ba);
}

void MainWindow::start_read_err_output()
{
    QByteArray ba = process->readAllStandardError();
    ui->textBrowser->append("error: " + ba);
}

void MainWindow::closeEvent(QCloseEvent *event)
{
    process->write("quit");
    process->kill();
    if(!process->waitForFinished(100))
        return;
    event->accept();
}

 

 

头文件:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QProcess>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    QProcess* process;

protected:
    void closeEvent(QCloseEvent *);

private slots:
    void on_pushButton_clicked();
    void start_process();
    void start_read_output();
    void start_read_err_output();
    void finish_process(int exitCode, QProcess::ExitStatus exitStatus);

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

 

最后执行结果:

bubuko.com,布布扣

 

【Qt】使用QProcess调用其它程序或脚本,布布扣,bubuko.com

【Qt】使用QProcess调用其它程序或脚本

原文:http://www.cnblogs.com/kernel0815/p/3923608.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!