[英]pyqt: widget with transparent background
I want to draw a transparent-background rectangle in pyqt.我想在 pyqt 中绘制一个透明背景矩形。 The following code meets my need in Mac:以下代码满足我在 Mac 中的需求:
import sys
from PyQt4 import QtGui, QtCore, Qt
class ZoomWidget(QtGui.QWidget):
def __init__(self):
QtGui.QWidget.__init__(self)
#self.setAttribute(Qt.Qt.WA_NoSystemBackground)
self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
self.setAttribute(QtCore.Qt.WA_TranslucentBackground)
self.setStyleSheet("background-color:transparent;")
self.setGeometry(100,100,100,100)
self.show()
def paintEvent(self, e=None):
qp = QtGui.QPainter()
qp.begin(self)
qp.setPen( QtGui.QPen(QtCore.Qt.gray,3,QtCore.Qt.DashDotLine ) )
qp.drawRect(0,0,self.rect().width()-1, self.rect().height()-1)
qp.end()
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
ex = ZoomWidget()
sys.exit(app.exec_())
However, when I used this code in Linux, it gave me a black-background rectangle.但是,当我在 Linux 中使用此代码时,它给了我一个黑色背景矩形。 What should I do to give the transparent background to it?我该怎么做才能给它透明背景? Thanks.谢谢。
have you tried to set parent of ZoomWidget
?您是否尝试过设置ZoomWidget
父ZoomWidget
?
...
def __init__(self, *args, **kwargs):
QtGui.QWidget.__init__(self, *args, **kwargs)
...
app = QtGui.QApplication(sys.argv)
dial = QtGui.QDialog()
ex = ZoomWidget(parent=dial)
dial.show()
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.