QOpenGLWidget and transparency
-
Hi. I have problems with
QOpenGLWidget
and transparency. Here is my code:#include <QApplication> #include <QOpenGLWidget> #include <QOpenGLFunctions> #include <QOpenGLFunctions_4_3_Core> #include <QDebug> class OpenGLWidget : public QOpenGLWidget { public: OpenGLWidget(QWidget *parent = 0, const QColor color = QColor(255, 175, 75)) : QOpenGLWidget(parent) { QSurfaceFormat format; format.setVersion(4, 3); format.setRedBufferSize(8); format.setGreenBufferSize(8); format.setBlueBufferSize(8); format.setAlphaBufferSize(8); format.setSamples(8); setFormat(format); this->create(); m_color = color; } protected: void initializeGL() { QOpenGLFunctions_4_3_Core *f = QOpenGLContext::currentContext()->versionFunctions<QOpenGLFunctions_4_3_Core>(); qDebug() << m_color; f->glClearColor(m_color.redF(), m_color.greenF(), m_color.blueF(), m_color.alphaF()); } void paintGL() { auto f = QOpenGLContext::currentContext()->versionFunctions<QOpenGLFunctions_4_3_Core>(); f->glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); } void resizeGL(int w, int h) { auto f = QOpenGLContext::currentContext()->versionFunctions<QOpenGLFunctions_4_3_Core>(); f->glViewport(0, 0, w, h); } private: QColor m_color; }; int main(int argc, char *argv[]) { QApplication a(argc, argv); OpenGLWidget w; w.show(); w.setMinimumSize(800, 600); OpenGLWidget w2(&w, QColor(125, 125, 125, 1)); w2.setAttribute(Qt::WA_TranslucentBackground); w2.setGeometry(w.width()/2 - 40, w.height()/2 - 40, 80, 80); w2.show(); return a.exec(); }
When I'm creating second widget (
w2
) I'm settings it's alpha to 1 (doesn't matter if I will set it to 0, or 100, or 255) but it looks like as it has 255. How can I solve this problem?I'm using Qt 5.5, mingw32 and windows 10
-
Hi,
It is a limitation.
http://doc.qt.io/qt-5/qopenglwidget.html
"Limitations
Putting other widgets underneath and making the QOpenGLWidget transparent will not lead to the expected results: The widgets underneath will not be visible. This is because in practice the QOpenGLWidget is drawn before all other regular, non-OpenGL widgets, and so see-through type of solutions are not feasible. Other type of layouts, like having widgets on top of the QOpenGLWidget, will function as expected."You have to use Qt::WA_AlwaysStackOnTop to do this.