You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Qt 6.6.1 WebAssembly中父窗口背景变黑问题求助

Qt6 WebAssembly平台添加OpenGL部件后父窗口背景变黑问题

在主窗口中添加OpenGL部件后,父窗口背景变为黑色:
WebAssembly平台异常截图

相关代码如下:

main.cpp

#include <QtGui/QOpenGLFunctions>
#include <QtOpenGLWidgets/QOpenGLWidget>
#include <QtWidgets/QApplication>
#include <QtWidgets/QLabel>
#include <QtWidgets/QVBoxLayout>
#include <QtWidgets/QWidget>

class OpenGLWidget : public QOpenGLWidget, private QOpenGLFunctions
{
    Q_OBJECT

private:
    void initializeGL() override
    {
        initializeOpenGLFunctions();
    }

    void paintGL() override
    {
        glClearColor(0.1f, 0.3f, 0.2f, 1.f);
        glClear(GL_COLOR_BUFFER_BIT);
    }
};

class MainWindow : public QWidget
{
    Q_OBJECT

public:
    MainWindow()
    {
        setWindowTitle("OpenGL, Qt6, C++");
        resize(300, 300);

        m_nameLabel = new QLabel("Click on object or background");
        m_nameLabel->setSizePolicy(QSizePolicy::Policy::Fixed, QSizePolicy::Policy::Fixed);
        OpenGLWidget *openGLWidget = new OpenGLWidget();

        QVBoxLayout *layout = new QVBoxLayout();
        layout->addWidget(m_nameLabel);
        layout->addWidget(openGLWidget);
        // layout->addStretch();
        setLayout(layout);
    }

private:
    QLabel *m_nameLabel;
};

#include "main.moc"

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    MainWindow w;
    w.show();
    return app.exec();
}

pro文件

QT += core gui openglwidgets widgets

win32: LIBS += -lopengl32

CONFIG += c++17

SOURCES += \
    main.cpp

TARGET = app

现象说明

  • 该问题仅出现在WebAssembly平台,桌面端运行正常:
    桌面端正常截图
  • Android物理设备运行也正常:
    Android设备正常截图
  • 若移除OpenGL部件(修改布局代码如下),窗口背景恢复正常:
    QVBoxLayout *layout = new QVBoxLayout();
    layout->addWidget(m_nameLabel);
    // layout->addWidget(openGLWidget);
    layout->addStretch();
    setLayout(layout);
    
    效果截图:
    移除OpenGL部件后正常截图

内容的提问来源于stack exchange,提问作者8Observer8

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 03:42:13