#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() << "外部程序窗口已嵌入。";
}
}
QT中调用exe(带参数和工作目录)
于 2024-03-01 15:33:21 首次发布
&spm=1001.2101.3001.5002&articleId=136388804&d=1&t=3&u=ad4ef2661b78427d9a5a4bdbaa69d3bc)
10万+

被折叠的 条评论
为什么被折叠?



