1.窗口整体透明,但是窗体上的控件不透明。
class BackRect(QtGui.QWidget): borderWidth = 200 borderHeight = 200 def __init__(self, parent = None): QtGui.QWidget.__init__(self, parent) self.resize(parent.width() - self.borderWidth , parent.height() -self.borderHeight) self.setAutoFillBackground(True) backcolor = QtGui.QPalette() clr = QtGui.QColor('red') clr.setAlpha(100) backcolor.setColor(QtGui.QPalette.Background, clr) self.setPalette(backcolor);
这段代码是我写的一个验证程序,有的网友说这个方法无法让其透明。我当时重载的是 QDialog 也无法做到让背景透明。用上面的方法无法做到透明的同学可以看看自已是不是在实例化该类时是没有指定父控件呢。
self.RollBox = BackRect(self.centralWidget)
2、通过设置窗体来实现,将背景色设置为全透。
def set_transparency(self, enabled): if enabled: self.setAutoFillBackground(False) else: self.setAttribute(Qt.WA_NoSystemBackground, False) self.setAttribute(Qt.WA_TranslucentBackground, enabled) self.repaint()
这个没有什么好说的,不能让背景半透明。
3.窗口及其上面的控件都半透明:
class RollBoxObject(QtGui.QDialog): borderWidth = 300 borderHeight = 200 def __init__(self, parent = None): QtGui.QDialog.__init__(self, parent) self.setWindowFlags(QtCore.Qt.FramelessWindowHint|QtCore.Qt.Dialog) self.setWindowOpacity(0.5) #设置背景透明 self.setAutoFillBackground(True) backcolor = QtGui.QPalette() backcolor.setColor(QtGui.QPalette.Background, QtGui.QColor(255, 0, 0)) self.setPalette(backcolor);
我用这个的时间使终没有好的办法让其子控件不透明。。。
以上代码同让式用与Qt