
我希望做一个有透明背景的QGLWidget,就像上图一样。
这是一个cpp代码,我不理解它的某些部分。据我所知,它使用了窗口句柄、绘图上下文等。但我不擅长C和C++。我用过Python和PyQt,所以它对我来说就像是亚特兰蒂斯。对于具有透明背景的QGLWidget有什么想法吗?
添加
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_()我尝试了上面的代码,但它什么都没有显示。(它看起来像是一个完全透明的小部件。)如果去掉"setAttribute","setWindowFlags“是一个三角形。代码中有什么遗漏吗?
发布于 2013-11-13 11:57:50
从this C++ solution翻译成Python。末尾的分号已被删除,::已替换为.,并且True已大写。困难的主要来源是setAttribute和setWindowFlags实际上是QGLWidget的方法,所以添加了self.。只需将此代码添加到您的QGLWidget`的__init__()构造函数中。
self.setAttribute(Qt.WA_TranslucentBackground, True)您可以添加self.setWindowFlags(Qt.FramelessWindowHint | Qt.WindowStaysOnTopHint)
如果您希望窗口位于其他窗口的顶部,并且没有框架。
https://stackoverflow.com/questions/19944636
复制相似问题