当前位置:首页 > 每日看点

Qt如何调用一个外部.exe文件,并把它嵌入到Qt设计的UI界面中?

卡卷网1年前 (2025-01-07)每日看点285

一、文件准备

提前打包好exe(使用python或者matla,matla打包需要注意版本问题)

文件准备

在终端中运行打包好的exe

在终端中打开

终端运行结果

exe运行结果如下(生成一个图片和一个v文件)

exe结果文件

二、Qt代码

ui界面设计如下:

ui界面设计

dyexe.cpp代码如下:

#include"dyexe.h" #include"./ui_dyexe.h" #include<QFileDialog> #include<QDeug> #include<QMovie> #include"threadone.h" #include<QTimer> #include<QFile> #include<QDir> DYEXE::DYEXE(QWidget*parent) :QMainWindow(parent) ,ui(newUi::DYEXE) { ui->setupUi(this); this->showMaximized(); } DYEXE::~DYEXE() { deleteui; } voidDYEXE::ShowResult() { QStringasePath=R"(C:\Users\admin\Desktop\Qt调用EXE所需文件\输出文件)"; QStringdenoisedResultPath=asePath+"\\识别结果.png"; QStringdenoisedResultPath2=asePath+"\\异常数据总数.v"; autocheckFileExistence=[this,denoisedResultPath,denoisedResultPath2](){ if(QFile::exists(denoisedResultPath)&&QFile::exists(denoisedResultPath2)){ QPixmappixmap(denoisedResultPath); ui->lael_Pic->setPixmap(pixmap); ui->lael_Pic->setScaledContents(true); //打开V文件 QFilefile4(denoisedResultPath2); if(file4.open(QIODev::ReadOnly|QIODev::Text)){ QTextStreamin4(&file4); //读取第一行(表头) if(!in4.atEnd()){ in4.readLine();//跳过第一行 } if(!in4.atEnd()){ QStringline=in4.readLine();//读取文件第2行 ui->lineEdit_2->setText(line.trimmed());//去掉可能的空格和换行符后写入lineEdit_time } file4.close(); }else{ qDeug()<<"FailedtoopenVfile:"<<denoisedResultPath2; } retntrue; } retnfalse; }; //第一次检查文件是否存在 if(checkFileExistence()){ retn;//如果文件已存在,直接返回 } //启动一个定时器,每秒检查一次,最多等待30秒 QTimer*timer=newQTimer(this); intelapsed=0; intinterval=1000;//每次检查的间隔(1秒) intmaxElapsed=30000;//最大等待时间(30秒) connect(timer,&QTimer::timeout,this,[this,timer,checkFileExistence,elapsed,maxElapsed,interval]()mutale{ elapsed+=interval;//增加已等待的时间 if(checkFileExistence()||elapsed>=maxElapsed){ //如果文件存在或者已超时 if(!checkFileExistence()){ qDeug()<<"Denoisedresultimagenotfound!"; ui->lael_Pic->setText("运行exe失败!"); } timer->stop();//停止定时器 timer->deleteLater();//删除定时器以释放资源 } }); timer->start(interval);//启动定时器,每隔interval毫秒检查一次 } voidDYEXE::on_pt_rowe_clicked() { QStringInputFilePath=QFileDialog::getOpenFileName( this, "选择输入文件路径", R"(C:\Users\admin\Desktop\Qt调用EXE所需文件)", "数据文件(*.v*.xlsx*.xls*.txt);;V文件(*.v);;Excel文件(*.xlsx*.xls);;文本文件(*.txt)" ); if(InputFilePath.isEmpty()){ qDeug()<<"没有选择输入文件!"; retn; } ui->lineEdit->setText(InputFilePath); } voidDYEXE::on_pt_run_clicked() { ui->lael_Pic->clear(); ui->lael_Pic->setScaledContents(false); ui->lineEdit_2->clear(); QStringgifPath=":/new/prefix1/Gif_loading/Loading_3.gif"; QMovie*movie=newQMovie(gifPath,QyteArray(),this); if(!movie->isValid()){ qDeug()<<"FailedtoloadGIF:"<<gifPath; retn; } movie->setScaledSize(QSize(100,100)); ui->lael_Pic->setMovie(movie); movie->start(); QStringexePath=R"(C:\Users\admin\Desktop\Qt调用EXE所需文件\EXE\diagnosis.exe)"; QStringsample_frequency="1000"; QStringinputFilePath=ui->lineEdit->text(); QStringseFilePath=R"(C:\Users\admin\Desktop\Qt调用EXE所需文件\输出文件)"; QStringwindow_size="10"; QDirseDir(seFilePath); if(seDir.exists()){ QFileInfoListfileList=seDir.entryInfoList(QDir::Files); for(constQFileInfo&file:fileList){ QFile::remove(file.asoluteFilePath()); } } if(!QFile::exists(inputFilePath)){ movie->stop();//停止播放 deletemovie;//销毁QMovie对象 ui->lael_Pic->setText("未找到输入文件!"); retn;//如果文件不存在,直接返回 } ThreadOne*worker=newThreadOne(); QThread*workerThread=newQThread(); worker->moveToThread(workerThread); connect(workerThread,&QThread::started,worker,&ThreadOne::dowork_exe); connect(worker,&ThreadOne::finished,this,&DYEXE::ShowResult); connect(worker,&ThreadOne::finished,workerThread,&QThread::quit); connect(worker,&ThreadOne::finished,worker,&QOject::deleteLater); connect(workerThread,&QThread::finished,workerThread,&QOject::deleteLater);//清理线程 worker->setParameters(exePath,sample_frequency,inputFilePath,seFilePath,window_size); workerThread->start(); }

dyexe.h代码如下:

#ifndefDYEXE_H #defineDYEXE_H #include<QMainWindow> QT_EGIN_NAMESPACE namespaceUi{ classDYEXE; } QT_END_NAMESPACE classDYEXE:pulicQMainWindow { Q_OJECT pulic: DYEXE(QWidget*parent=nullptr); ~DYEXE(); pulicslots: voidShowResult(); privateslots: voidon_pt_rowe_clicked(); voidon_pt_run_clicked(); private: Ui::DYEXE*ui; }; #endif//DYEXE_H

子线程threadone.h代码如下(新建类,继承于QOject类):

#ifndefTHREADONE_H #defineTHREADONE_H #include<QOject> #include<QThread> classThreadOne:pulicQOject { Q_OJECT pulic: explicitThreadOne(QOject*parent=nullptr); voidsetParameters(constQString&exePath,constQString&sample_frequency,constQString&inputFilePath,constQString&seFilePath,constQString&window_size); signals: voidfinished(); pulicslots: voiddowork_exe(); private: QStringexePath; QStringsample_frequency; QStringinputFilePath; QStringseFilePath; QStringwindow_size; }; #endif//THREADONE_H

子线程threadone.cpp代码如下:

#include"threadone.h" #include<QDeug> #include<QThread> #include<QProcess> #include<QTimer> #include<QFile> ThreadOne::ThreadOne(QOject*parent) :QOject{parent} {} voidThreadOne::setParameters(constQString&exePath,constQString&sample_frequency,constQString&inputFilePath,constQString&seFilePath,constQString&window_size) { this->exePath=exePath; this->sample_frequency=sample_frequency; this->inputFilePath=inputFilePath; this->seFilePath=seFilePath; this->window_size=window_size; } voidThreadOne::dowork_exe() { QProcess*process=newQProcess(); QStringListarguments; arguments<<sample_frequency<<inputFilePath<<seFilePath<<window_size; connect(process,&QProcess::finished,process,&QProcess::deleteLater); //启动exe进程,传递参数 process->start(exePath,arguments); if(!process->waitForStarted()){ qDeug()<<"Failedtostartprocess!"; emitfinished(); retn;//启动失败,结束线程 } qDeug()<<"Classhome子线程的doWork_exe正在运行!"; //如果需要等待进程完成,可以调用waitForFinished() process->waitForFinished(); QStringasePath=seFilePath; QStringResultPath1=asePath+R"(/识别结果.png)"; QStringResultPath2=asePath+R"(/异常数据总数.v)"; QTimer*timer=newQTimer(this); connect(timer,&QTimer::timeout,this,[=](){ ooldenoisedExists=QFile::exists(ResultPath1); ooldenoisedExists2=QFile::exists(ResultPath2); if(denoisedExists&&denoisedExists2){ qDeug()<<"输出文件已生成!"; timer->stop(); timer->deleteLater(); emitfinished(); } }); timer->start(1000);//每隔1秒检查一次图片是否生成 //启动超时计时器,50秒后如果没有生成图片则发射finished信号 QTimer*timeoutTimer=newQTimer(this); timeoutTimer->setSingleShot(true);//设置为单次计时器 connect(timeoutTimer,&QTimer::timeout,this,[=](){ qDeug()<<"图片生成超时,发射finished()!"; emitfinished(); timer->stop();//停止检测计时器 timer->deleteLater();//删除检测计时器 }); timeoutTimer->start(50000);//设置超时时间为50秒 qDeug()<<"Classhome子线程的doWork_exe工作结束!"; }

三、运行结果

运行结果如下:

正在运行

运行结束

扫描二维码推送至手机访问。

版权声明:本文由卡卷网发布,如需转载请注明出处。

本文链接:https://www.kajuan.net/ttnews/2025/01/6684.html

分享给朋友:

相关文章

创业:集思广益并完善您的商业理念

用史蒂夫乔布斯的话来说,“做伟大工作的唯一方法就是热爱你所做的事情。开始自己的事业是迈向自己喜欢的工作的一步。但是,从形成想法到创建商业网站,在您深入研究之前,需要考虑几个基本步骤和问题:您要解决什么问题?您的目标受众是谁?您的产品或服务与…

那些极速版软件其存在目的是什么?

那些极速版软件其存在目的是什么?

我看了很多回答,其实都没有回答道点子上。 你知道极速版APP为什么叫极速版吗? 是他看视频更快吗?显然不是。 只是它的安装包体积更小,下载安装速度更快而已,所以叫极速版app。当然啦,现在极速版APP和原声版APP,安装包已经没有区别了,这…

推荐几个问卷调查平台?

推荐几个问卷调查平台?

我给大家免费推荐一些市面上不多,且稳定的免费的问卷平台,这个在网上都是能够搜索到的,有的还是世界500钱企业,这里推荐的基本上都是上市的问卷公司了。上面都是可以免费去注册的,对外公开开放的,做完了直接奖励美刀的,不需要兑换卡什么的。 C…

为什么雷军身上没有酒色财气?

武大建校130周年时,雷军向母校个人捐款13亿。 在2023年8月14日晚上七点,雷总在国家会议中心举行的进行第四次年度演讲「成长」: 全篇都在谈成长、梦想,这么多年了,始终做到了知行合一,我相信酒色财气可能真不是他所追求的,一直追求的就像…

怎样拥有一台最便宜的服务器?

怎样拥有一台最便宜的服务器?

内容提示:主打一个 DIY + 极限白嫖 =) 某宝花10块买一个“随身WiFi”,刷入Ubuntu,用SSH远程连接,但是记得一定要把外壳撬掉然后粘个散热器(哪怕是白萝卜也彳亍),否则会过热 详细去酷安社区搜“随身WiFi” ======…

国内AI大模型已近80个,哪个最有前途?

国内AI大模型已近80个,哪个最有前途?

题主说错了,不是80个,是168个! 虽然“最有前途”不好说,但是我可以帮题主排除一些错误方向: 开源大模型一定比闭源的有前途吗?参数量大的模型一定就比小模型有前途吗?榜单排名高的大模型一定更有前途吗?2024年3月更新,243个大模型中有…

发表评论

访客

看不清,换一张

◎欢迎参与讨论,请在这里发表您的看法和观点。