当前位置:网站首页>QT调用外部程序
QT调用外部程序
2022-04-23 13:25:00 【微小冷】
QT实战教程:
源码地址: QT调用外部程序
调用系统指令
针对类似复制、移动等功能,显然操作系统提供了更加完备的指令。QT
提供了QProcess
类,用于调用系统指令,就像C语言中的system
一样。
例如,在Windows
中也提供了复制命令copy
,那么拖动一个按钮,改名为btnCopyFile
,然后转到槽,添加click
动作
void MainWindow::on_btnCopyFile_clicked()
{
QString oldName = QFileDialog::getOpenFileName(this,"请选择文件");
QString newName = QFileDialog::getSaveFileName(this,"请输入文件名");
QProcess cpPro(this);
QString code = "copy";
ui->txtContent->setText(code);
QStringList para;
para << oldName << newName;
foreach(QString item, para)
ui->txtContent->append(item);
cpPro.start(code, para);
}
其中,cpPro
就是一个QProcess,最终调用时用到cpPro.start(code,para)
,其中code
是准备执行的指令,格式为QString
;para
是指令列表,类型为QStringList
,也就是QList<QString>
。
效果为
系统指令返回值
QProcess
的强大之处在于,不仅可以执行系统的命令行指令,还能捕获命令行的返回值。以ls
命令为例(当然,Windows自身是没有ls的,可用dir)
其效果为
代码为
void MainWindow::on_btnListFiles_clicked()
{
QString folder = QFileDialog::getExistingDirectory(this,"请选择文件夹");
ui->lineTitle->setText(folder);
QProcess cmd;
cmd.start("dir",QStringList()<<folder);
cmd.waitForStarted();
cmd.waitForFinished();
ui->txtContent->append(cmd.readAllStandardOutput());
}
和之前的代码相比,增加了两个wait
,一个read
,顾名思义,前者分别用于等待命令开启和完成,后者用于获取命令行的标准输出。
这里涉及到QProcess的生命周期:
waitForStarted()
:阻塞,直到外部程序启waitForReadyRead()
:阻塞,直到输出通道中的新数据可读waitForBytesWritten()
:阻塞,直到输入通道中的数据被写入waitForFinished()
:阻塞,直到外部程序结束
版权声明
本文为[微小冷]所创,转载请带上原文链接,感谢
https://tinycool.blog.csdn.net/article/details/124359078
边栏推荐
- 2020最新Android大厂高频面试题解析大全(BAT TMD JD 小米)
- Remove the status bar
- kettle庖丁解牛第16篇之输入组件周边讲解
- 面试官给我挖坑:单台服务器并发TCP连接数到底可以有多少 ?
- [point cloud series] deepmapping: unsupervised map estimation from multiple point clouds
- AUTOSAR from introduction to mastery 100 lectures (87) - key weapon of advanced EEA - AUTOSAR and DDS
- Brief introduction of asynchronous encapsulation interface request based on uniapp
- 9419页最新一线互联网Android面试题解析大全
- Xi'an CSDN signed a contract with Xi'an Siyuan University, opening a new chapter in IT talent training
- Ffmpeg common commands
猜你喜欢
@Excellent you! CSDN College Club President Recruitment!
TERSUS笔记员工信息516-Mysql查询(2个字段的时间段唯一性判断)
[51 single chip microcomputer traffic light simulation]
Ding ~ your scholarship has arrived! C certified enterprise scholarship list released
MySQL 8.0.11下载、安装和使用可视化工具连接教程
你和42W奖金池,就差一次“长沙银行杯”腾讯云启创新大赛!
100 lectures on practical application cases of Excel (VIII) - report connection function of Excel
The first lesson is canvas, showing a small case
叮~ 你的奖学金已到账!C认证企业奖学金名单出炉
./gradlew: Permission denied
随机推荐
./gradlew: Permission denied
这几种 VSCode 扩展是我最喜欢的
You and the 42W bonus pool are one short of the "Changsha bank Cup" Tencent yunqi innovation competition!
C语言之字符串与字符数组的区别
mui 微信支付 排坑
2021年6月程序员工资统计,平均15052元,你拖后腿了吗?
vscode小技巧
Common interview questions and detailed analysis of the latest Android developers in 2020
AUTOSAR from introduction to mastery 100 lectures (86) - 2F of UDS service foundation
Mui + hbuilder + h5api simulate pop-up payment style
[quick platoon] 215 The kth largest element in the array
hbuilderx + uniapp 打包ipa提交App store踩坑记
AUTOSAR from introduction to mastery 100 lectures (50) - AUTOSAR memory management series - ECU abstraction layer and MCAL layer
Remove the status bar
[point cloud series] multi view neural human rendering (NHR)
FatFs FAT32 learning notes
Solve the problem that Oracle needs to set IP every time in the virtual machine
The first lesson is canvas, showing a small case
CMSIS cm3 source code annotation
@优秀的你!CSDN高校俱乐部主席招募!