当前位置:首页 > 每日看点 > 正文内容

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

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

一、文件准备

提前打包好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

分享给朋友:

相关文章

手机买16+256的还是12+512的好?

手机买16+256的还是12+512的好?

到底选大内存还是大存储,一直以来是很多朋友的纠结点,大内存意味着可以应用多开不卡顿,大存储则是可以存入更多文件、应用,如果两个配置价格差距不大(如0-200元),确实有点难选。 不过从实际体验出发,大存储的手机显然更应该优先选择才是。首先,...

想要在双 11 换一台全面无短板的新手机,有没有「闭眼买」的机型推荐?

想要在双 11 换一台全面无短板的新手机,有没有「闭眼买」的机型推荐?

最近一个月各大手机厂商的旗舰机扎堆发布,不知道大家看爽了没?这一代的性能续航大提升,最低 3599 元就能买到,同时老款也有不小的降幅,今年双 11 算是相当适合换手机的节点了!这次,小黑就给大家推荐双 11 期间值得购买的手机...150...

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

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

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

为什么不建议买游戏本?

我建议厂家改名厂家犯了个巨大错误:游戏本的主要受众是在校大学生,但大学生买电脑的买单人是他们的父母。你让父母给孩子买个游戏本,父母怎么想?如果你改成高效学习本、优质学习本、智能学习本、高速学习本,甚至硕博连读本,你看看家长舍不舍得砸这个钱...

面试官问“你的SQL能力怎么样?”时应该如何回答?

面试官问“你的SQL能力怎么样?”时应该如何回答?

如果面试时被问你SQL能力怎么样?要么是考查一下题目,怎么写SQL语句。要么是问SQL优化相关的,更容易问出一个人的水平。sql优化是一个大家都比较关注的热门话题,无论你在面试,还是工作中,都很有可能会遇到。如果某天你负责的某个线上接口,出...

作为一名网络工程师,每天日常工作是什么样的?

作为一名网络工程师,每天日常工作是什么样的?

先介绍下我的基本情况,本人女,不知名的网络工程师一枚,入行也有三四年了。虽然我只在代理商和集成商干过,但怎么也算是“业内人士”一枚,没吃过猪肉总见过猪跑嘛。今天,就以我经历过,或者了解过的网络工程师,来给大家介绍一下到底每天的工作日常是什么...

发表评论

访客

看不清,换一张

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