QT如何调用外部.exe文件(该.exe文件调用了其他的.o.n文件)

    科技2022-07-11  67

    首先感谢博主 https://blog.csdn.net/pyuxing/article/details/89494504

    第一步就是在QTcreator里面创建个新工程,在这个新工程目录下 点击进入下图的.ui设计 在上图里面设计的你UI界面模型。记住对文本框和摁键的命名和代码命名一致,在摁键选择右键添加槽然后mainwindow.cpp里面就会产生一个下面函数,在这函数里面添加你要实现的功能代码。

    在博主代码基础上调试了能独立运行hellow.exe C++ hellow.cpp代码如下

    #include "stdafx.h" #include "windows.h" #include <iostream> #include <string> using namespace std; int main(int argc, char* argv[]) { Sleep(1000); std::cout << "Hello CodeLab!" << std::endl; cout << "laozi 501515.1258 4848.18 41484.15641 fdg dfsgdf sgfds " << endl; Sleep(1000); cout << "laozi tianxia wudi" << endl; Sleep(1000); cout << "laozi tianxia wudi" << endl; Sleep(1000); cout << "laozi tianxia wudi" << endl; return 0; }

    QT mainwindow.cpp代码如下`

    #include "mainwindow.h" #include "ui_mainwindow.h" #include <qdebug.h> #include <qprocess.h> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow){ ui->setupUi(this); } MainWindow::~MainWindow(){ delete ui; } void MainWindow::on_btnPing_clicked(){ qDebug()<<QStringLiteral("开始点击"); ui->textBrowser->clear(); ui->textBrowser->setText(QStringLiteral("请等待")); ui->textBrowser->update(); QProcess myProcess(this); QString program = "C:\\test.exe"; QStringList arguments; arguments<<"1";//传递到exe的参数 myProcess.start(program,arguments); while (myProcess.waitForFinished(100) == false) { QByteArray qByteRead = myProcess.readAllStandardOutput(); if (!qByteRead.isEmpty()) { ui->textBrowser->append(QString::fromLocal8Bit(qByteRead)); repaint(); } } QByteArray qByteRead = myProcess.readAllStandardOutput(); ui->textBrowser->append(QString::fromLocal8Bit(qByteRead)); qDebug()<<QString::fromLocal8Bit(qByteRead); qDebug()<<"结束点击"; }`

    运行结果如下

    上面的例子.exe不需要依赖外部文件,比如.exe需要用到其他文件(.o .n)里面的数据,那么就需要到那个目录下启用进程。

    误打误撞找到了setWorkingDirectory函数,不清楚怎么使用这个函数的小伙伴请翻译一下这下面的图片 调用下面两个语句 QProcess myProcess(this);

    myProcess.setWorkingDirectory(“放文件目录”) 后面的就正常使用myProcess.start(program);

    5.QTcreator里面有一个函数挺好用的那就是 qDebug() << tr(“timer.exe启动成功”); qDebug()<<“还没结束”; 这个现实函数对初学者还是很有帮助的。

    Processed: 0.009, SQL: 8