当前位置:网站首页>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
边栏推荐
- 你和42W奖金池,就差一次“长沙银行杯”腾讯云启创新大赛!
- [dynamic programming] 221 Largest Square
- 5道刁钻的Activity生命周期面试题,学完去吊打面试官!
- Uninstall MySQL database
- C语言之字符串与字符数组的区别
- Is Hongmeng system plagiarism? Or the future? Professional explanation that can be understood after listening in 3 minutes
- [multi screen interaction] realize dual multi screen display II: startactivity mode
- Mui + hbuilder + h5api simulate pop-up payment style
- [point cloud series] summary of papers related to implicit expression of point cloud
- Common analog keys of ADB shell: keycode
猜你喜欢
EMMC / SD learning notes
集简云 x 飞书深诺,助力企业运营部实现自动化办公
MySQL 8.0.11下载、安装和使用可视化工具连接教程
GIS practical tips (III) - how to add legend in CASS?
The filter() traverses the array, which is extremely friendly
TERSUS笔记员工信息516-Mysql查询(2个字段的时间段唯一性判断)
【快排】215. 数组中的第K个最大元素
RTOS mainstream assessment
How do ordinary college students get offers from big factories? Ao Bing teaches you one move to win!
你和42W奖金池,就差一次“长沙银行杯”腾讯云启创新大赛!
随机推荐
Loading and using image classification dataset fashion MNIST in pytorch
MySQL 8.0.11 download, install and connect tutorials using visualization tools
playwright控制本地穀歌瀏覽打開,並下載文件
Imx6ull QEMU bare metal tutorial 2: usdhc SD card
Is Hongmeng system plagiarism? Or the future? Professional explanation that can be understood after listening in 3 minutes
uniapp image 引入本地图片不显示
[point cloud series] full revolutionary geometric features
这几种 VSCode 扩展是我最喜欢的
torch. Where can transfer gradient
playwright控制本地谷歌浏览打开,并下载文件
交叉碳市场和 Web3 以实现再生变革
2021年6月程序员工资统计,平均15052元,你拖后腿了吗?
[point cloud series] pointfilter: point cloud filtering via encoder decoder modeling
初鉴canvas,展示个小小的小案例
torch. Where can transfer gradient
基于uniapp异步封装接口请求简介
web三大组件之Filter、Listener
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?
kettle庖丁解牛第16篇之输入组件周边讲解
Solve the problem that Oracle needs to set IP every time in the virtual machine