QT:打开外部程序

本文介绍了在Qt中通过QProcess的startDetached函数和WINAPI的shellExecute函数启动外部程序的方法。提供了具体的代码示例,并强调了在不加载配置文件的情况下需要指定工作路径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 使用QProcess的startDetached函数。

startDetached函数有三种原型:
    static bool startDetached(const QString &program, const QStringList &arguments,
                              const QString &workingDirectory
    static bool startDetached(const QString &program, const QStringList &arguments); 
    static bool startDetached(const QString &command);
    #include <QProcess>
    QStringList strList;//参数list
    strList << "1" << "2";//所需传入的命令行参数,不需参数则置空即可
    QProcess::startDetached("D://02_wind//main//Wind.exe", strList, "D://02_wind//main");
注意:在使用过程中,所打开的exe文件不加载配置文件,则需要指定工作路径,即将workingDirectory参数补充上。

2.使用WINAPI的shellExecute函数。

函数原型:
HINSTANCE ShellExecute(          
    HWND hwnd,
    LPCTSTR lpOperation,
    LPCTSTR lpFile,
    LPCTSTR lpParameters,
    LPCTSTR lpDirectory,
    INT nShowCmd
);
#include <shellapi.h>
#include <ShlObj.h>
#include <qt_windows.h>
ShellExecute(NULL, L"open", L"D://02_wind//main//Wind.exe", NULL, L"D://02_wind//main", SW_SHOW);
注意:在使用过程中,所打开的exe文件不加载配置文件,则需要指定工作路径,即将lpDirectory参数补充上。
### 实现方案 为了实现在 Qt 应用程序中启动外部进程并将其窗口集成到 GUI 中的功能,可以采用 `QProcess` 类来管理子进程,并利用平台特定的方式获取和嵌入目标窗口。对于 Linux 环境下的具体操作如下: #### 使用 QProcess 启动外部应用程序 可以通过创建 `QProcess` 对象实例化一个新的进程对象,设置要执行的命令及其参数,最后调用 start 方法启动该进程。 ```cpp // 创建一个 QProcess 对象用于处理外部程序 QProcess *process = new QProcess(this); QString program = "./externalApp"; // 替换为实际路径 QStringList arguments; // 如果有参数则加入列表 // 设置工作目录(可选) process->setWorkingDirectory("/path/to/workdir"); // 连接信号槽以便监控进程状态变化 connect(process, SIGNAL(finished(int,QProcess::ExitStatus)), this,SLOT(onProcessFinished(int,QProcess::ExitStatus))); connect(process, SIGNAL(error(QProcess::ProcessError)), this,SLOT(onProcessErrorOccurred(QProcess::ProcessError))); // 开始执行指定程序 process->start(program, arguments); if (!process->waitForStarted()) { qDebug() << "Failed to start process."; } ``` 上述代码展示了如何使用 C++Qt 的方式去启动一个名为 `externalApp` 的外部应用[^1]。 #### 获取外部窗口句柄并在 Qt 中显示 在 Linux 上,通常会涉及到 X Window System (X11),因此需要借助于 Xlib 或者更现代的库如 xcb 来获得新创建窗口的信息。一旦获得了窗口 ID,则可通过 QWidget 提供的相关接口将此窗口作为子控件添加进来。 ```cpp #include <QWidget> #include <QX11Info> // For accessing the display connection on X11 platforms. #ifdef Q_OS_LINUX extern "C" { #include <X11/Xlib.h> } #endif void MainWindow::embedExternalWindow(WId winId) { #ifdef Q_OS_LINUX Display* dpy = QX11Info::display(); Window w = static_cast<Window>(winId); // Reparenting window into our widget's native handle reparent_window(dpy, w, effectiveWinId()); updateGeometry(); // Force layout recalculation after embedding. #else qWarning("Embedding external windows is only supported under Linux."); #endif } bool MainWindow::reparent_window(Display* display, Window child, WId parent) { XReparentWindow(display, child, parent, 0, 0); // Move and attach it as a child of 'this' XRaiseWindow(display, child); // Bring embedded window above others within its container XFlush(display); // Ensure all commands are sent immediately return true; } ``` 这段代码片段说明了怎样通过 XLib 函数重新定位已存在的窗口至当前 Qt 小部件内部,从而达到视觉上的嵌套效果。 请注意以上示例假设读者已经熟悉基本的 Qt 编程概念以及所使用的开发环境配置正确无误。此外,由于不同版本间可能存在 API 差异,请参照官方文档确认最新语法细节。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值