Qt QProcess嵌入外部exe程序

更新一下程序(嵌套窗口鼠标滚轮事件无效解决)

h头文件

//加载Unity3D窗口

void loadWindowUnity3DExe(std::function f);

cpp文件

/**

* 加载Unity3D窗口

* @brief loadWindowUnity3DExe

* @return

*/

void loadWindowUnity3DExe(std::function f)

{

    QString cmd = QDir::currentPath()+"/model0714/motioncapture.exe";

    QFileInfo file(cmd);

    if(file.exists()==false){

        f(0);

        return;

    }

    //启动exe程序

    QProcess myprocess;

    myprocess.startDetached(cmd,QStringList());

    WId wid = 0;

    do

    {

        QEventLoop loop;

        QTimer::singleShot(1, &loop, SLOT(quit()));

        loop.exec();

        wid = (WId)FindWindow(L"UnityWndClass",L"motioncapture");

    }while(wid == 0);

    f(wid);

}

mainwindow.cpp文件

//加载Unity3D

    loadWindowUnity3DExe([=](WId wid){

        //代表文件不存在或者没找到wid

        if(wid == 0){

            QMessageBox::warning(this,"提示",messagelist[4]);

        }else {

            QWindow *m_window= QWindow::fromWinId(wid);

            m_window->setFlags(m_window->flags() | Qt::CustomizeWindowHint | Qt::WindowTitleHint);

            QWidget *unityWidget = QWidget::createWindowContainer(m_window,ui->centralwidget);

            //unityWidget->setAttribute(Qt::WA_QuitOnClose);

            setCentralWidget(unityWidget);

        }

    });

以下是最新的内容,嵌套的窗体支持鼠标事件,具体如下:

首先是新建mainwindow, 自定义主窗口widget嵌套窗体后,不确定鼠标事件有效。

通过QProcess开启嵌套窗口并添加

QProcess *myprocess = new QProcess;

    QString cmd = QDir::currentPath()+"/model0714/motioncapture.exe";

    QFileInfo file(cmd);

    if(file.exists()){

        QStringList arguments;

        arguments<<"-style"<<"fusion";

        myprocess->setWorkingDirectory(QDir::currentPath()+"/model0714");

        myprocess->setProcessChannelMode(QProcess::MergedChannels);

        myprocess->start(cmd, arguments);

        WId wid = 0;

        do

        {

            QEventLoop loop;

            QTimer::singleShot(1, &loop, SLOT(quit()));

            loop.exec();

            wid = (WId)FindWindow(L"UnityWndClass",L"motioncapture");

        }while(wid == 0);

        QWindow *m_window= QWindow::fromWinId(wid);

        m_window->setFlags(m_window->flags() | Qt::CustomizeWindowHint | Qt::WindowTitleHint | Qt::FramelessWindowHint);

        m_window->requestActivate();

        QWidget *unityWidget = QWidget::createWindowContainer(m_window,leftSplitter);

        unityWidget->setFocusPolicy(Qt::StrongFocus);

//鼠标事件有效代码

        AttachThreadInput(GetWindowThreadProcessId(::GetForegroundWindow(),NULL), GetCurrentThreadId(),TRUE);

        SetForegroundWindow(HWND(wid));

        SetFocus(HWND(wid));

        AttachThreadInput(GetWindowThreadProcessId(::GetForegroundWindow(),NULL), GetCurrentThreadId(),FALSE);

    }

你可能感兴趣的:(Qt QProcess嵌入外部exe程序)