QT QProcess的使用

2024-03-21 17:18
文章标签 使用 qt qprocess

本文主要是介绍QT QProcess的使用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1、主程序:初始化及设定信号槽

 

process = new QProcess();

 

connect(process,SIGNAL(started()),SLOT(started()));

 

connect(process,SIGNAL(finished(int,QProcess::ExitStatus)),SLOT(finished()));

 

connect(process,SIGNAL(stateChanged(QProcess::ProcessState)),SLOT(stateChanged()));

2、主程序:启动process

 

// QStringList list;

// list.append("hello_1");

// list.append("world_2");

// list.append("ok_3");

QStringList list;

list<<"hello_1"<<"world_2"<<"ok_3";

 

QString program = "E:\\hit-qt\\build-TestCallTo-Desktop_Qt_5_8_0_MinGW_32bit-Debug\\debug\\TestCallTo.exe";

 

process->start(program,list);

3、主程序:注意start和startDetached的区别

    process->startDetached(QString("E:\\hit-qt\\build-TestCallTo-Desktop_Qt_5_8_0_MinGW_32bit-Debug\\debug\\TestCallTo.exe"),list);

start是一体式的:外部程序启动后,将随主程序的退出而退出;

startDetached是分离式的:外部程序启动后,不会随主程序的退出而退出。

重要区别:如果是start则回调都可以正常接收到信息;如果是startDetached则回调无法正常接收到信息。

4、主程序:只有在外部程序退出之后才能获取到返回数据

经测试,只有在外部程序返回之后才能获取到不管是main的返回值,还是打印输出数据。

使用标准输出,任何时候都可以获得返回:

std::cout<<"it's from cout"<<std::endl;

 

5、主程序:获取main返回值

建立连接:

    connect(process,SIGNAL(finished(int,QProcess::ExitStatus)),SLOT(finished(int,QProcess::ExitStatus)));

回调:

 

void Widget::finished(int exitCode,QProcess::ExitStatus exitStatus)

{

qDebug()<<"finished";

 

qDebug()<<exitCode;// 被调用程序的main返回的int

qDebug()<<exitStatus;// QProcess::ExitStatus(NormalExit)

qDebug() <<"finished-output-readAll:";

qDebug()<<QString::fromLocal8Bit(process->readAll());

qDebug()<<"finished-output-readAllStandardOutput:";

qDebug()<<QString::fromLocal8Bit(process->readAllStandardOutput());

 

}

6、主程序:获取返回输出流

建立连接:

 

connect(process,SIGNAL(finished(int,QProcess::ExitStatus)),SLOT(finished(int,QProcess::ExitStatus)));

 

connect(process,SIGNAL(readyRead()),this,SLOT(readyRead()));

 

connect(process,SIGNAL(readyReadStandardOutput()),this,SLOT(readyReadStandardOutput()));

回调:

 

void Widget::finished(int exitCode,QProcess::ExitStatus exitStatus)

{

qDebug()<<"finished";

 

qDebug()<<exitCode;// 被调用程序的main返回的int

qDebug()<<exitStatus;// QProcess::ExitStatus(NormalExit)

qDebug() <<"finished-output-readAll:";

qDebug()<<QString::fromLocal8Bit(process->readAll());// ""

qDebug()<<"finished-output-readAllStandardOutput:";

qDebug()<<QString::fromLocal8Bit(process->readAllStandardOutput());// ""

}

void Widget::readyRead()

{

qDebug()<<"readyRead-readAll:";

qDebug()<<QString::fromLocal8Bit(process->readAll());// "hello it is ok!"

qDebug()<<"readyRead-readAllStandardOutput:";

qDebug()<<QString::fromLocal8Bit(process->readAllStandardOutput());// ""

}

void Widget::readyReadStandardOutput()

{

qDebug()<<"readyReadStandardOutput-readAll:";

qDebug()<<QString::fromLocal8Bit(process->readAll());// ""

qDebug()<<"readyReadStandardOutput-readAllStandardOutput:";

qDebug()<<QString::fromLocal8Bit(process->readAllStandardOutput());// ""

}

经测试发现,只有在readyRead回调下面使用readAll来读取,才可以读取到数据。

 

4、外部程序:获取main接收参数

 

// 在这里打印参数

QString str1 = QString("These are the %1 arguments passed to main:").arg(argc);

for(int i=1;i<argc;i++)

{

QString strN = QString("%1:%2,").arg(i).arg(argv[i]);

str1+=strN;

}

 

w.SetText(str1);

 

5、外部程序:返回main参数

自定义返回的参数。程序会在a.exec()阻塞并在程序结束后才会执行return。

a.exec();// 会在这里阻塞return 101;

6、外部程序:返回数据

一句话即可。

要用这个:

std::cout<<"it's from cout"<<std::endl;

不要用这个:

    printf("hello it is ok!");

7、process其他:stateChanged的各种状态

    connect(process,SIGNAL(stateChanged(QProcess::ProcessState)),SLOT(stateChanged(QProcess::ProcessState)));

回调:

void Widget::stateChanged(QProcess::ProcessState state)

{

qDebug()<<"show state:";

switch(state)

{

case QProcess::NotRunning:

qDebug()<<"Not Running";

break;

case QProcess::Starting:

qDebug()<<"Starting";

break;

case QProcess::Running:

qDebug()<<"Running";

break;

default:

qDebug()<<"otherState";

break;

}

}

8、process其他:调用命令行

 

void Widget::testPing()

{

QStringList arguments;

arguments<<"/c"<<"ping www.baidu.com";//

 

QProcess process1(this);

process1.start("cmd.exe",arguments);

process1.waitForStarted();

process1.waitForFinished();

QString strResult = QString::fromLocal8Bit(process1.readAllStandardOutput());

 

QMessageBox msgBox(this);

msgBox.setText(strResult);

msgBox.exec();

}

9、判断启动成功或失败

 

process = new QProcess(parent);

 

process->start("TestCallTo.exe",NULL);

 

if(!process->waitForStarted())

{

qDebug()<<"failure!";

}else

{

qDebug()<<"succ!";

}

这篇关于QT QProcess的使用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/833334

相关文章

Python数据验证神器Pydantic库的使用和实践中的避坑指南

《Python数据验证神器Pydantic库的使用和实践中的避坑指南》Pydantic是一个用于数据验证和设置的库,可以显著简化API接口开发,文章通过一个实际案例,展示了Pydantic如何在生产环... 目录1️⃣ 崩溃时刻:当你的API接口又双叒崩了!2️⃣ 神兵天降:3行代码解决验证难题3️⃣ 深度

Linux内核定时器使用及说明

《Linux内核定时器使用及说明》文章详细介绍了Linux内核定时器的特性、核心数据结构、时间相关转换函数以及操作API,通过示例展示了如何编写和使用定时器,包括按键消抖的应用... 目录1.linux内核定时器特征2.Linux内核定时器核心数据结构3.Linux内核时间相关转换函数4.Linux内核定时

python中的flask_sqlalchemy的使用及示例详解

《python中的flask_sqlalchemy的使用及示例详解》文章主要介绍了在使用SQLAlchemy创建模型实例时,通过元类动态创建实例的方式,并说明了如何在实例化时执行__init__方法,... 目录@orm.reconstructorSQLAlchemy的回滚关联其他模型数据库基本操作将数据添

Spring配置扩展之JavaConfig的使用小结

《Spring配置扩展之JavaConfig的使用小结》JavaConfig是Spring框架中基于纯Java代码的配置方式,用于替代传统的XML配置,通过注解(如@Bean)定义Spring容器的组... 目录JavaConfig 的概念什么是JavaConfig?为什么使用 JavaConfig?Jav

Java使用Spire.Doc for Java实现Word自动化插入图片

《Java使用Spire.DocforJava实现Word自动化插入图片》在日常工作中,Word文档是不可或缺的工具,而图片作为信息传达的重要载体,其在文档中的插入与布局显得尤为关键,下面我们就来... 目录1. Spire.Doc for Java库介绍与安装2. 使用特定的环绕方式插入图片3. 在指定位

Springboot3 ResponseEntity 完全使用案例

《Springboot3ResponseEntity完全使用案例》ResponseEntity是SpringBoot中控制HTTP响应的核心工具——它能让你精准定义响应状态码、响应头、响应体,相比... 目录Spring Boot 3 ResponseEntity 完全使用教程前置准备1. 项目基础依赖(M

Java使用Spire.Barcode for Java实现条形码生成与识别

《Java使用Spire.BarcodeforJava实现条形码生成与识别》在现代商业和技术领域,条形码无处不在,本教程将引导您深入了解如何在您的Java项目中利用Spire.Barcodefor... 目录1. Spire.Barcode for Java 简介与环境配置2. 使用 Spire.Barco

Android使用java实现网络连通性检查详解

《Android使用java实现网络连通性检查详解》这篇文章主要为大家详细介绍了Android使用java实现网络连通性检查的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录NetCheck.Java(可直接拷贝)使用示例(Activity/Fragment 内)权限要求

C# 预处理指令(# 指令)的具体使用

《C#预处理指令(#指令)的具体使用》本文主要介绍了C#预处理指令(#指令)的具体使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学... 目录1、预处理指令的本质2、条件编译指令2.1 #define 和 #undef2.2 #if, #el

C#中Trace.Assert的使用小结

《C#中Trace.Assert的使用小结》Trace.Assert是.NET中的运行时断言检查工具,用于验证代码中的关键条件,下面就来详细的介绍一下Trace.Assert的使用,具有一定的参考价值... 目录1、 什么是 Trace.Assert?1.1 最简单的比喻1.2 基本语法2、⚡ 工作原理3