QT中调用exe(带参数和工作目录)

#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include <QDebug>
#include <QProcess>
#include <Windows.h>
#include <QTimer>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
    , process(nullptr) // 初始化为 nullptr
    , timer(new QTimer(this)) // 创建定时器
{
    ui->setupUi(this);
    timer->setInterval(100); // 设置定时器间隔为100毫秒
    connect(timer, &QTimer::timeout, this, &MainWindow::checkExternalAppWindow);
}

MainWindow::~MainWindow()
{
    delete ui;
    if (process) {
        process->kill(); // 确保外部进程被终止
        delete process;
    }
}

void MainWindow::on_openGaoSi_clicked()
{
    qDebug() << "打开高斯可视化Viewer...";

    // 创建QProcess实例并设置工作目录
    QProcess *process = new QProcess(this);
    QString workingDirectory = "C:/chenqi/splatapult/build/Release";
    process->setWorkingDirectory(workingDirectory);

    // 定义程序路径和参数
    QString program = "C:/chenqi/splatapult/build/Release/splatapult.exe";
    QStringList arguments;
    arguments << "C:/chenqi/splatapult/build/Release/data/test.ply";

    // 启动程序并传递参数
    process->start(program, arguments);

    timer->start(); // 启动定时器,开始周期性检查外部程序窗口
}

void MainWindow::checkExternalAppWindow()
{
    HWND hExeWnd = ::FindWindow(NULL, "splatapult"); // 使用实际的窗口标题
    if (hExeWnd) {
        timer->stop(); // 找到窗口,停止定时器

        ::SetParent(hExeWnd, (HWND)this->winId());
        ::SetWindowPos(hExeWnd, HWND_TOP, 0, 0, 400, 300, SWP_NOZORDER);
        qDebug() << "外部程序窗口已嵌入。";
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

江河地笑

实践是检验真理的唯一标准

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值