我尝试使用createWindowContainer()将UE4窗口嵌入Qt。它成功了,但仍然存在多个问题:
鼠标光标在错误的位置
如图所示,主Qt窗口与屏幕左上角的距离为(30,30),而UE4窗口与Qt主窗口的偏移为(100,50)。在UE4窗口内的鼠标光标也应从其应有的位置偏移(30,30)。
我打印出UE4代码SWindow :: GetPositionInScreen()。独立运行UE4窗口时,它返回屏幕左上角的偏移量。 但是从Qt主窗口运行UE4时,它仅返回距Qt主窗口左上角的偏移量(100,50),并且应该返回(100,50)+(30,30)=(130,80)。 我该怎么做才能使其正确?
我的代码如下:
class CanvasWidget : public QWidget
{
Q_OBJECT
public:
CanvasWidget(QWidget *parent = nullptr);
virtual ~CanvasWidget();
void mouseMoveEvent(QMouseEvent *event) override;
void mousePressEvent(QMouseEvent *event) override;
bool event(QEvent *event) override;
bool eventFilter(QObject *obj, QEvent *event);
void enterEvent(QEvent *event) override;
void leaveEvent(QEvent *event) override;
};
class canvasWindow : public QWindow
{
Q_OBJECT
public:
canvasWindow(QWidget *parent = nullptr);
~canvasWindow();
void mouseMoveEvent(QMouseEvent *event) override;
void mousePressEvent(QMouseEvent *event) override;
bool event(QEvent *event) override;
bool eventFilter(QObject *obj, QEvent *event);
void enterEvent(QEvent *event);
void leaveEvent(QEvent *event);
};
main.cpp
QWindow* ueQWindow = QWindow::fromWinId((WId)m_hwnUE4Window);
CanvasWindow* ue4Window = (CanvasWindow*)ueQWindow;
QWidget* nestedUEQWidget = QWidget::createWindowContainer(ue4Window , ptr2QtMainWindow);
CanvasWidget* nestedUEWidget = (CanvasWidget*)nestedUEQWidget;
nestedUEWidget->setMouseTracking(true);