当前位置:网站首页>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
边栏推荐
- AUTOSAR from introduction to mastery 100 lectures (86) - 2F of UDS service foundation
- 超40W奖金池等你来战!第二届“长沙银行杯”腾讯云启创新大赛火热来袭!
- 解决Oracle中文乱码的问题
- 【快排】215. 数组中的第K个最大元素
- uniapp image 引入本地图片不显示
- Solve the problem that Oracle needs to set IP every time in the virtual machine
- Filter and listener of three web components
- 【官宣】长沙软件人才实训基地成立!
- Uninstall MySQL database
- Lpddr4 notes
猜你喜欢
Data warehouse - what is OLAP
[point cloud series] unsupervised multi task feature learning on point clouds
Loading and using image classification dataset fashion MNIST in pytorch
X509 parsing
数据仓库—什么是OLAP
Servlet of three web components
vscode小技巧
2020年最新字节跳动Android开发者常见面试题及详细解析
AUTOSAR from introduction to mastery 100 lectures (52) - diagnosis and communication management function unit
Nodejs + websocket cycle small case
随机推荐
AUTOSAR from introduction to mastery 100 lectures (83) - bootloader self refresh
Isparta is a tool that generates webp, GIF and apng from PNG and supports the transformation of webp, GIF and apng
ECDSA signature verification principle and C language implementation
Nodejs + Mysql realize simple registration function (small demo)
playwright控制本地谷歌浏览打开,并下载文件
[dynamic programming] 221 Largest Square
AUTOSAR from introduction to mastery 100 lectures (51) - AUTOSAR network management
交叉碳市场和 Web3 以实现再生变革
100 GIS practical application cases (52) - how to keep the number of rows and columns consistent and aligned when cutting grids with grids in ArcGIS?
【行走的笔记】
Mysql数据库的卸载
The filter() traverses the array, which is extremely friendly
100 GIS practical application cases (34) - splicing 2020globeland30
nodejs + mysql 实现简单注册功能(小demo)
mui 微信支付 排坑
MySQL5.5安装教程
浅谈js正则之test方法bug篇
What do the raddr and rport in webrtc ice candidate mean?
[point cloud series] unsupervised multi task feature learning on point clouds
CSDN高校俱乐部“名师高校行”——湖南师范大学站