当前位置:网站首页>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
边栏推荐
- 超40W奖金池等你来战!第二届“长沙银行杯”腾讯云启创新大赛火热来袭!
- ./gradlew: Permission denied
- Lpddr4 notes
- @优秀的你!CSDN高校俱乐部主席招募!
- Loading and using image classification dataset fashion MNIST in pytorch
- [Journal Conference Series] IEEE series template download guide
- 解决虚拟机中Oracle每次要设置ip的问题
- 2020最新Android大厂高频面试题解析大全(BAT TMD JD 小米)
- Nodejs + Mysql realize simple registration function (small demo)
- Analysis of the latest Android high frequency interview questions in 2020 (BAT TMD JD Xiaomi)
猜你喜欢
web三大组件之Servlet
Android clear app cache
mui + hbuilder + h5api模拟弹出支付样式
超40W奖金池等你来战!第二届“长沙银行杯”腾讯云启创新大赛火热来袭!
AUTOSAR from introduction to mastery 100 lectures (86) - 2F of UDS service foundation
MySQL5. 5 installation tutorial
filter()遍历Array异常友好
How do ordinary college students get offers from big factories? Ao Bing teaches you one move to win!
2020最新Android大厂高频面试题解析大全(BAT TMD JD 小米)
初鉴canvas,展示个小小的小案例
随机推荐
playwright控制本地谷歌浏览打开,并下载文件
Scons build embedded ARM compiler
榜样专访 | 孙光浩:高校俱乐部伴我成长并创业
[andorid] realize SPI communication between kernel and app through JNI
CSDN College Club "famous teacher college trip" -- Hunan Normal University Station
SPI NAND flash summary
nodeJs + websocket 循环小案例
[point cloud series] Introduction to scene recognition
[dynamic programming] 221 Largest Square
Using open to open a file in JNI returns a - 1 problem
100 GIS practical application cases (51) - a method for calculating the hourly spatial average of NC files according to the specified range in ArcGIS
AUTOSAR from introduction to mastery 100 lectures (83) - bootloader self refresh
Migrating your native/mobile application to Unified Plan/WebRTC 1.0 API
Common commands of ADB shell
Common interview questions and detailed analysis of the latest Android developers in 2020
What do the raddr and rport in webrtc ice candidate mean?
ECDSA signature verification principle and C language implementation
Machine learning -- naive Bayes
X509 parsing
[point cloud series] foldingnet: point cloud auto encoder via deep grid deformation