Zk.Sun 2022-05-16 16:30 采纳率: 50%
浏览 54
已结题

ubuntu 下Qt程序嵌入外部程序窗口,无法嵌入的问题

问题遇到的现象和发生背景

最进需要将一个窗口通过Qt嵌入到主窗口作为显示,但是发现只能打开需要嵌入的窗口程序,不能将其窗口嵌入到主窗口中,求解

问题相关代码,请勿粘贴截图

以写是网上通过进程id获取窗口的方法

#include <stdlib.h>
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <iostream>
#include <list>





class WindowsMatchingPid
{
public:
    WindowsMatchingPid(Display *display, Window wRoot, unsigned long pid)
        : _display(display),
          _pid(pid)
    {
        // Get the PID property atom.
        _atomPID = XInternAtom(display, "_NET_WM_PID", True);
        if(_atomPID == None)
        {
            std::cout << "No such atom" << std::endl;
            return;
        }

        search(wRoot);
    }

    const std::list<Window> &result() const { return _result; }

private:
    unsigned long  _pid;
    Atom           _atomPID;
    Display       *_display;
    std::list<Window>   _result;

    void
    search(Window w)
    {
        // Get the PID for the current Window.
        Atom           type;
        int            format;
        unsigned long  nItems;
        unsigned long  bytesAfter;
        unsigned char *propPID = 0;
        if(Success == XGetWindowProperty(
                    _display,
                    w,
                    _atomPID,
                    0,
                    1,
                    False,
                    XA_CARDINAL,
                    &type,
                    &format,
                    &nItems,
                    &bytesAfter,
                    &propPID))
        {
            if(propPID != 0)
            {
                // If the PID matches, add this window to the result set.
                if(_pid == *((unsigned long *)propPID))
                {
                    _result.push_back(w);
                }

                XFree(propPID);
            }
        }

        // Recurse into child windows.
        Window    wRoot;
        Window    wParent;
        Window   *wChild;
        unsigned  nChildren;
        if(0 != XQueryTree(_display, w, &wRoot, &wParent, &wChild, &nChildren))
        {
            for(unsigned i = 0; i < nChildren; i++)
                search(wChild[i]);
        }

//        XFree(propPID);
    }
};


unsigned long
get_win_id_from_pid(qint64 pid)
{
    //std::cout << "Searching for windows associated with PID " << pid << std::endl;

    // Start with the root window.
    Display *display = XOpenDisplay(0);

    WindowsMatchingPid match(display, XDefaultRootWindow(display), pid);

    // Print the result.
    const std::list<Window> &result = match.result();

    long win_id = 0;
    for(std::list<Window>::const_iterator pos = result.begin(); pos != result.end(); pos++)
    {
        std::cout << "Window #" << (unsigned long)(*pos) << std::endl;
        win_id = (unsigned long)(*pos);
    }

    return win_id;
}

以下是显示该窗口的实现

    m_process = new QProcess(this);

#ifdef Q_OS_WIN
    m_process->start("D:\\CARLA_0.9.11\\WindowsNoEditor\\CarlaUE4\\Binaries\\Win64\\CarlaUE4-Win64-Shipping.exe");
#else
//    m_process->start("/home/jyc/carla/Dist/CARLA_Shipping_0.9.13-1-g8854804f4-dirty/LinuxNoEditor/CarlaUE4.sh");
    m_process->start("/home/jyc/workSpace/Qt/build-testA-Desktop_Qt_5_13_2_GCC_64bit-Debug/testA");
#endif
//    m_process->start("D:\\CARLA_0.9.11\\WindowsNoEditor\\CarlaUE4\\Binaries\\Win64\\run.bat");
//      m_process->start("D:\\CARLA_0.9.11\\WindowsNoEditor\\CarlaUE4.exe");
//    m_process->start("D:\\myworkspace\\build-testA-Desktop_Qt_5_13_2_MinGW_32_bit-Release\\release\\testA.exe");

    if(m_process != nullptr)
    {
        qint64 testAID = m_process->processId();
        printf("%d",testAID);
        fflush(stdout);


        QThread::sleep(2);
        WId id = get_win_id_from_pid(testAID);


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



        QWidget *m_widget;
        m_widget = QWidget::createWindowContainer(m_window,this);

        m_widget->winId();
        m_widget->setParent(this);

        ui->verticalLayout->addWidget(m_widget);
    }

运行结果及报错内容

无法正常嵌入窗口

img


两个mainwindow是分开显示的,并非嵌入进去的

我的解答思路和尝试过的方法
我想要达到的结果

希望达到嵌入效果

  • 写回答

0条回答 默认 最新

    报告相同问题?

    问题事件

    • 系统已结题 5月24日
    • 创建了问题 5月16日

    悬赏问题

    • ¥20 校园二手交易小程序搭建
    • ¥15 请问在ubuntu用conda创建环境报错怎么能解决
    • ¥15 STM32CubeMX/proteus按键控制指示灯颜色切换
    • ¥20 python,计算区位熵和扩张指数
    • ¥15 Python环境配置
    • ¥15 大四学生的困惑,有偿提问!
    • ¥15 解决页面无法编入索引:被“noindex”标签排除的问题?
    • ¥15 arduino测量电阻
    • ¥15 快手uid转快手号谁能解决 需要开发
    • ¥15 iis部署Django时css不生效,来个真人,ai不好使