QT筆記——QProcess學習

我們常常想通過某一個類,來啟動一個外部進程

本文將講解如何通過QProcess來進行啟動外部進程

一:了解QProcess

QProcess是Qt框架提供的一個類,用于在應用程序中執行外部進程。它提供了一系列函數來啟動、控制和與外部進程進行交互

1.啟動進程的方式:
(1.1)分離式:外部程序啟動程序后,主程序退出時,被調用的進程繼續執行,不退出
請添加圖片描述

[static] bool QProcess::startDetached(const QString &program, const QStringList &arguments, const QString &workingDirectory = QString(), qint64 *pid = nullptr)

(1.2)一體式:當主程序退出時,被主程序調用起來的進程也退出

start還有其他的重載的函數,下面是其中一個
請添加圖片描述

void QProcess::start(const QString &program, const QStringList &arguments, QIODevice::OpenMode mode = ReadWrite)

2:常用的阻塞函數

// 主進程阻塞,直到外部程序啟動完畢,
waitForStarted()// 主進程阻塞,直到外部程序執行完畢
waitForFinished() 

3:被調用進程接受數據


4:主進程接受被調用進程的數據
(4.1)使用QFile輸出內容被 主進程捕獲

QFile file;
file.open(1, QFile::WriteOnly);
file.write("finished");
file.close();

(4.2)使用輸出流 被 主進程捕獲

 cout << "it's message" << endl;

5:常用的信號
請添加圖片描述

//啟動完畢
connect(process, &QProcess::started, this, [=]()
{});//捕獲到消息時
connect(process, &QProcess::readyReadStandardOutput, this, [=]()
{});//完成
connect(process, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished), this, [=](int exitCode, QProcess::ExitStatus exitStatus)
{});//狀態改變
connect(process, &QProcess::stateChanged, this, [=](QProcess::ProcessState state)
{});

二:使用QPrecess

1:startDetached 啟動進程

1:使用了startDetached來啟動進程,信號都是無效的,接受不到任何消息
2:當主進程關閉時,被調用的QTcpClientTest.exe 是不會退出的
3:主進程發送的參數,被調用的QTcpClientTest.exe 依然可以接受

void QTcpSeverTest::on_btn1_clicked()
{QProcess* process = new QProcess(this);QString str = QApplication::applicationDirPath() + "/QTcpClientTest.exe";QStringList list;list.append("123");list.append("456");//分離式 啟動外部進程process->startDetached(str, list);//無效connect(process, &QProcess::started, this, [=](){qDebug() << "started";});//無效connect(process, &QProcess::readyReadStandardOutput, this, [=](){QString qstr(process->readAllStandardOutput());qDebug() << "startDetached:" << qstr;});connect(process, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished), this, [=](int exitCode, QProcess::ExitStatus exitStatus){QString result = process->readAll();qDebug() << "result:" << result;});//無效connect(process, &QProcess::stateChanged, this, [=](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;}});}

2:start啟動進程

1:使用了startDetached來啟動進程,信號都是可以接受的
2:當主進程關閉時,被調用的QTcpClientTest.exe 是隨著主進程一起退出
3:主進程發送的參數,被調用的QTcpClientTest.exe 可以接受參數

	QProcess* process = new QProcess(this);QString str = QApplication::applicationDirPath() + "/QTcpClientTest.exe";QStringList list;list.append("bbbbb");list.append("aaaaa");//啟動完畢connect(process, &QProcess::started, this, [=](){qDebug() << "started:" ;});//捕獲到消息時connect(process, &QProcess::readyReadStandardOutput, this, [=](){QString qstr(process->readAllStandardOutput());qDebug() << "readyReadStandardOutput:" << qstr;});//完成connect(process, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished), this, [=](int exitCode, QProcess::ExitStatus exitStatus){if (exitStatus == QProcess::NormalExit) {qDebug() << "Process finished with exit code:" << exitCode;}else {qDebug() << "Process crashed!";}});//狀態改變connect(process, &QProcess::stateChanged, this, [=](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;}});//一體式  啟動外部進程process->start(str, list);

3:execute()啟動進程

1:使用了execute來啟動進程,信號是無效的
2:主進程一直處于阻塞狀態,等待被調用的QTcpClientTest.exe完成(關閉軟件)

	QProcess* process = new QProcess(this);QString str = QApplication::applicationDirPath() + "/QTcpClientTest.exe";QStringList list;list.append("bbbbb");list.append("aaaaa");//無效connect(process, &QProcess::started, this, [=](){qDebug() << "started:";});//無效connect(process, &QProcess::readyReadStandardOutput, this, [=](){QString qstr(process->readAllStandardOutput());qDebug() << "readyReadStandardOutput:" << qstr;});//無效connect(process, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished), this, [=](int exitCode, QProcess::ExitStatus exitStatus){QString result = process->readAll();qDebug() << "result:" << result;});//無效connect(process, &QProcess::stateChanged, this, [=](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;}});//如果進程 QTcpClientTest 不關閉 或者完成 , 則此進程一直卡住int exitCode = QProcess::execute(str, list);if (exitCode != 0) {qDebug() << "外部程序執行失敗";}//接受進程捕獲到輸出的消息QByteArray output = process->readAllStandardOutput();QString msg = QString::fromLocal8Bit(output);qDebug() << msg;

打印的信息為:QIODevice::read (QProcess): device not open,說明execute沒有打開設備,接受不到消息

4:start() + waitForFinished()啟動進程

1:使用了execute來啟動進程,信號是無效的
2:主進程一直處于阻塞狀態,等待被調用的QTcpClientTest.exe完成(關閉軟件)

	QProcess* process = new QProcess(this);QString str = QApplication::applicationDirPath() + "/QTcpClientTest.exe";QStringList list;list.append("bbbbb");list.append("aaaaa");//啟動進程process->start(str,list);// 等待進程完成process->waitForFinished();//接受進程捕獲到輸出的消息QByteArray output = process->readAllStandardOutput();QString msg = QString::fromLocal8Bit(output);qDebug() << msg;

完整代碼:
QProcess完整學習代碼

參考博客:
QProcess使用 一
QProcess使用 二
QProcess使用 三
QProcess使用 四

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/38296.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/38296.shtml
英文地址,請注明出處:http://en.pswp.cn/news/38296.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

C++入門基礎(萬字詳解!!!)

文章目錄 前言1.C關鍵字2.命名空間3.C的輸入輸出4.缺省參數4.1 全缺省4.2 半缺省 5.函數重載6. 引用6.1 引用的特性6.2 引用的使用場景6.3 引用和指針 7.內聯函數7.1 特性 8.auto關鍵字8.1 注意事項 9. 基于范圍的for循環9.1 使用條件 10.指針控制nullptr10.1 注意事項 11.總結…

fastadmin 自定義搜索分類和時間范圍

1.分類搜索&#xff0c;分類信息獲取----php 2.對應html頁面&#xff0c;頁面底部加搜索提交代碼&#xff08;這里需要注意&#xff1a;紅框內容&#xff09; 圖上代碼----方便直接復制使用 <script id"countrySearch" type"text/html"><!--form…

安全 1自測

常見對稱加密算法&#xff1a; DES&#xff08;Data Encryption Standard&#xff09;&#xff1a;數據加密標準&#xff0c;速度較快&#xff0c;適用于加密大量數據的場合&#xff1b; 3DES&#xff08;Triple DES&#xff09;&#xff1a;是基于DES&#xff0c;對一塊數據用…

Spring相關面試題

&#x1f44f;作者簡介&#xff1a;大家好&#xff0c;我是愛寫博客的嗯哼&#xff0c;愛好Java的小菜鳥 &#x1f525;如果感覺博主的文章還不錯的話&#xff0c;請&#x1f44d;三連支持&#x1f44d;一下博主哦 &#x1f4dd;個人博客&#xff1a;敬請期待 &#x1f4d5;系列…

時序預測 | MATLAB實現EEMD-GRU、GRU集合經驗模態分解結合門控循環單元時間序列預測對比

時序預測 | MATLAB實現EEMD-GRU、GRU集合經驗模態分解結合門控循環單元時間序列預測對比 目錄 時序預測 | MATLAB實現EEMD-GRU、GRU集合經驗模態分解結合門控循環單元時間序列預測對比效果一覽基本介紹模型搭建程序設計參考資料 效果一覽 基本介紹 1.MATLAB實現EEMD-GRU、GRU時…

springcloud+nacos實現灰度發布

灰度發布 gateway網關實現灰度路由 灰度發布實體 package com.scm.boss.common.bean;import lombok.Data; import lombok.experimental.Accessors;import java.io.Serializable;/*** 灰度發布實體*/ Data Accessors(chain true) public class GrayBean implements Serializ…

【Linux】—— 進程程序替換

目錄 序言 &#xff08;一&#xff09;替換原理 1、進程角度——見見豬跑 1?? 認識 execl 函數 2、程序角度——看圖理解 &#xff08;二&#xff09;替換函數 1、命名理解 2、函數理解 1??execlp 2??execv 3??execvp 4??execle 5??execve 6??execve…

機器學習重要內容:特征工程之特征抽取

目錄 1、簡介 2、?為什么需要特征工程 3、特征抽取 3.1、簡介 3.2、特征提取主要內容 3.3、字典特征提取 3.4、"one-hot"編碼 3.5、文本特征提取 3.5.1、英文文本 3.5.2、結巴分詞 3.5.3、中文文本 3.5.4、Tf-idf ?所屬專欄&#xff1a;人工智能 文中提…

LLaMA長度外推高性價比trick:線性插值法及相關改進源碼閱讀及相關記錄

前言 最近&#xff0c;開源了可商用的llama2&#xff0c;支持長度相比llama1的1024&#xff0c;拓展到了4096長度&#xff0c;然而&#xff0c;相比GPT-4、Claude-2等支持的長度&#xff0c;llama的長度外推顯得尤為重要&#xff0c;本文記錄了三種網絡開源的RoPE改進方式及相…

Vue-打印組件頁面

場景: 需要將頁面的局部信息打印出來&#xff0c;只在前端實現&#xff0c;不要占用后端的資源。經過百度經驗&#xff0c;決定使用 print-js和html2canvas組件。 1. 下載包 npm install print-js --save npm install --save html2canvas 2. 組件內引用 <script>impo…

C語言之數組指針和指針數組

C語言之數組指針和指針數組 一、含義二、定義2.1 指針數組2.2 數組指針 三、使用3.1 指針數組在參數傳遞時的使用3.1.1 指針數組的排序3.2 數組指針在參數傳遞時的使用 一、含義 指針數組&#xff1a;顧名思義&#xff0c;其為一個數組&#xff0c;數組里面存放著多個指針&…

C#生成隨機驗證碼

以下是一個簡單的C#驗證碼示例&#xff1a; private void GenerateCaptcha() {// 生成隨機字符串string chars "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";Random random new Random();string captchaString new string(Enumerable.Repe…

TPAMI, 2023 | 用壓縮隱逆向神經網絡進行高精度稀疏雷達成像

CoIR: Compressive Implicit Radar | IEEE TPAMI, 2023 | 用壓縮隱逆向神經網絡進行高精度稀疏雷達成像 注1:本文系“無線感知論文速遞”系列之一,致力于簡潔清晰完整地介紹、解讀無線感知領域最新的頂會/頂刊論文(包括但不限于Nature/Science及其子刊;MobiCom, Sigcom, MobiSy…

Java【算法 04】HTTP的認證方式之DIGEST認證詳細流程說明及舉例

HTTP的認證方式之DIGEST 1.是什么2.認值流程2.1 客戶端發送請求2.2 服務器返回質詢信息2.2.1 質詢參數2.2.2 質詢舉例 2.3 客戶端生成響應2.4 服務器驗證響應2.5 服務器返回響應 3.算法3.1 SHA-2563.1.1 Response3.1.2 A13.1.3 A2 3.2 MD53.2.1 Request-Digest3.2.2 A13.2.3 A2…

CSS3 中新增了哪些常見的特性?

聚沙成塔每天進步一點點 ? 專欄簡介? 圓角&#xff08;Border Radius&#xff09;? 漸變&#xff08;Gradients&#xff09;? 陰影&#xff08;Box Shadow&#xff09;? 文本陰影&#xff08;Text Shadow&#xff09;? 透明度&#xff08;Opacity&#xff09;? 過渡&…

Spring boot與Spring cloud 之間的關系

Spring boot與Spring cloud 之間的關系 Spring boot 是 Spring 的一套快速配置腳手架&#xff0c;可以基于spring boot 快速開發單個微服務&#xff0c;Spring Boot&#xff0c;看名字就知道是Spring的引導&#xff0c;就是用于啟動Spring的&#xff0c;使得Spring的學習和使用…

MATLAB中xlsread函數用法

目錄 語法 說明 示例 將工作表讀取到數值矩陣 讀取元胞的范圍 讀取列 請求數值、文本和原始數據 對工作表執行函數 請求自定義輸出 局限性 xlsread函數的功能是讀取Microsoft Excel 電子表格文件 語法 num xlsread(filename) num xlsread(filename,sheet) num x…

Nacos和GateWay路由轉發NotFoundException: 503 SERVICE_UNAVAILABLE “Unable to find

問題再現&#xff1a; 2023-08-15 16:51:16,151 DEBUG [reactor-http-nio-2][CompositeLog.java:147] - [dc73b32c-1] Encoding [{timestampTue Aug 15 16:51:16 CST 2023, path/content/course/list, status503, errorService Unavai (truncated)...] 2023-08-15 16:51:16,17…

leetcode27—移除元素

思路&#xff1a; 參考26題目雙指針的思想&#xff0c;只不過這道題不是快慢指針。 看到示例里面數組是無序的&#xff0c;也就是說后面的元素也是可能跟給定 val值相等的&#xff0c;那么怎么處理呢。就想到了從前往后遍歷&#xff0c;如果left對應的元素 val時&#xff0c…

汽車制造業上下游協作時 外發數據如何防泄露?

數據文件是制造業企業的核心競爭力&#xff0c;一旦發生數據外泄&#xff0c;就會給企業造成經濟損失&#xff0c;嚴重的&#xff0c;可能會帶來知識產權剽竊損害、名譽傷害等。汽車制造業&#xff0c;會涉及到重要的汽車設計圖紙&#xff0c;像小米發送汽車設計圖紙外泄事件并…