2

enter image description here

I hope to make a QGLWidget that has a transparent background, like a figure above.
It's a cpp code and I don't understand the some parts of it. As far as I know, it uses window handle, drawing context, and so on. But I'm poor at C and C++. I've used Python and PyQt, so it seems like a Atlantis for me. Is there any idea for QGLWidget that has a transparent background?

ADD

from PyQt4.QtCore import Qt
from PyQt4.QtGui import QApplication, QColor
from PyQt4.QtOpenGL import QGLWidget

from OpenGL import GL
import sys

class TransGLWidget(QGLWidget):
    def __init__(self, parent=None):
        QGLWidget.__init__(self, parent)
        self.setWindowFlags(Qt.FramelessWindowHint)
        self.setAttribute(Qt.WA_TranslucentBackground, True)
        self.setGeometry(200, 100, 640, 480)

    def initializeGL(self):
        self.qglClearColor(QColor(0, 0, 0, 0))

    def resizeGL(self, w, h):
        GL.glViewport(0, 0, w, h)
        GL.glMatrixMode(GL.GL_PROJECTION)
        GL.glLoadIdentity()
        x = float(w) / h
        GL.glFrustum(-x, x, -1.0, 1.0, 1.0, 10.0)
        GL.glMatrixMode(GL.GL_MODELVIEW)
        GL.glLoadIdentity()

    def paintGL(self):
        GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT)   

        GL.glBegin(GL.GL_TRIANGLES)
        GL.glColor3f(1.0, 0.0, 0.0)
        GL.glVertex3f(-1.0, 1.0, -3.0)
        GL.glColor3f(0.0, 1.0, 0.0)
        GL.glVertex3f(1.0, 1.0, -3.0)
        GL.glColor3f(0.0, 0.0, 1.0)
        GL.glVertex3f(0.0, -1.0, -3.0)
        GL.glEnd()

app = QApplication(sys.argv)
widget = TransGLWidget()    
widget.show()
app.exec_()

I try with above code, but it shows me nothing. (It seems a whole transparent widget.) If I get rid of "setAttribute", "setWindowFlags", is shows up a triangle. Is there any miss at the code?

Hyun-geun Kim
  • 919
  • 3
  • 22
  • 37
  • possible duplicate of [Qt Widget with Transparent Background](http://stackoverflow.com/questions/7667552/qt-widget-with-transparent-background) – Ramchandra Apte Nov 13 '13 at 04:01

1 Answers1

1

Translated into Python from this C++ solution. The semicolon at the end has been removed, :: has been replaced with ., and True has been capitalized. The main source of difficulty is that setAttribute and setWindowFlags are actually methods of the QGLWidget, so self. has been added. Just add this in the __init__() constructor of your QGLWidget`.

self.setAttribute(Qt.WA_TranslucentBackground, True)

You can add self.setWindowFlags(Qt.FramelessWindowHint | Qt.WindowStaysOnTopHint)

if you want the window to be on top of others and have no frame.

Ramchandra Apte
  • 4,033
  • 2
  • 24
  • 44