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

代码:

#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();
} 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();
//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();
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())
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 = );
~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

最后执行结果:

【Qt】使用QProcess调用其它程序或脚本的更多相关文章

  1. QT中QProcess调用命令行的痛苦经历(调用Winrar,设置工作目录,获得输出,注意引号与括号,等等)

    QT中QProcess调用命令行的痛苦经历   阅读目录 创建压缩包的方法 在QT中调用命令行 在QT中调用C++创建的dll 在QT程序中需要将某些目录和文件压缩为一个rar的压缩包,于是想到了在Q ...

  2. python调用其他程序或脚本方法(转)

    python运行(调用)其他程序或脚本 在Python中可以方便地使用os模块运行其他的脚本或者程序,这样就可以在脚本中直接使用其他脚本,或者程序提供的功能,而不必再次编写实现该功能的代码.为了更好地 ...

  3. QT中QProcess调用命令行的痛苦经历

    在QT程序中需要将某些目录和文件压缩为一个rar的压缩包,于是想到了在QT中通过QProcess类调用命令行的rar.exe来达到效果,但是没想到QProcess类用起来很麻烦,而且达不到效果,折腾了 ...

  4. Qt 中用QProcess调用cmd命令

    项目做到一定阶段,常常须要在原来的project上调用外部程序. Qt为此提供了QProcess类,QProcess可用于完毕启动外部程序,并与之交互通信. 基本用法: QProcess p(0); ...

  5. qt 程序中执行额外程序和脚本

    1.最简单的,我们可以通过system直接启动一个应用程序或者脚本:(但是要调用 #include <stdlib.h>) system("./helloworld") ...

  6. Linux打包免安装的Qt程序(编写导出依赖包的脚本copylib.sh,程序启动脚本MyApp.sh)

    本文介绍如何打包Qt程序,使其在没有安装Qt的系统可以运行. 默认前提:另外一个系统和本系统是同一个系统版本. 1,编写导出依赖包的脚本copylib.sh #!/bin/bash LibDir=$P ...

  7. windows下Qt Creator5.1.0编写程序以及调用OpenCV库

    系统说明 最近使用opencv编写程序,程序编的差不多就学习使用QT加个界面,首先声明下本人的系统和使用的软件版本, 系统: windows xp QT IDE:QT Creator5.1.0 Ope ...

  8. Android: 通过Runtime.getRuntime().exec调用底层Linux下的程序或脚本

    Android Runtime使得直接调用底层Linux下的可执行程序或脚本成为可能 比如Linux下写个测试工具,直接编译后apk中通过Runtime来调用 或者写个脚本,apk中直接调用,省去中间 ...

  9. QT学习之第一个程序

    QT学习之第一个程序 目录 手动创建主窗口 居中显示 添加窗口图标 显示提示文本 Message Box的应用 手动连接信号与槽 手动创建主窗口 窗口类型 QMainWindow: 可以包含菜单栏.工 ...

随机推荐

  1. Java Volatile相关文章目录

    参考资料: http://www.google.com/cse?sa.x=0&sa.y=0&cx=010284515138798138769%3Aajbqkpwaapm&ie= ...

  2. 里德九步审讯法 z

    在现实生活中,警方审讯靠的不仅仅是自信和创造力(尽管这两点对审讯工作确有帮助)——审讯者还要在交际影响的心理战术方面接受过高水平训练.       让一个人认罪可不是件容易事,而警察有时能让无辜者承认 ...

  3. jquery validate.addMethod 正则表达式

    $(document).ready(function () { /* 设置默认属性 */ $.validator.setDefaults( { submitHandler: function (for ...

  4. [SQL]sql介绍

    SQL语言概述 结构化查询语言(Structured Query Language,简称SQL)是一种介于关系代数与关系演算之间的语言,是一种用来与关系数据库管理系统通信的标准计算机语言.其功能包括数 ...

  5. iframe 跨域自适应 纯css解决方法

    <style type="text/css">body{background:#f00;}body, html,#ifm1{width:100%;height:100% ...

  6. (medium)LeetCode 210.Course Schedule II

    There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...

  7. 一个关于js的内存问题

    <script type="text/javascript"> function textChange(id, fn) { var textarea = documen ...

  8. this.Invoke

    this.Invoke(new Action(() =>      {              }));

  9. IOS开发-phonegap上的数据库

    phonegap提供的本地数据库功能不可用,最终请教高手后使用SQLitePlugin插件来实现.网上很多都是介绍IOS插件如何编程,少有介绍如何使用插件: 一.插件安装: 1.下载,解压,复制以下文 ...

  10. 使用升级版的 Bootstrap typeahead v1.2.2

    上次介绍了 Bootstrap 2 中附带的 typeahead,功能强大,但是使用起来不太方便,作者 Terry Rosen 已经升级了一个新版本 v1.2.2,作出了很大的改进. 下载地址 htt ...