问题遇到的现象和发生背景
最进需要将一个窗口通过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);
}
运行结果及报错内容
无法正常嵌入窗口
两个mainwindow是分开显示的,并非嵌入进去的
我的解答思路和尝试过的方法
我想要达到的结果
希望达到嵌入效果