如何为绘制的对象(例如 drawLine)制作透明背景? [英] how to make transparent background for drawn objects (ex. drawLine)?
问题描述
我想制作一个应用程序,其中只有绘制的对象(例如 drawLine)可见,而不是背景.
I want to make to make an application where just the drawn objects (ex. drawLine) is visible, not the background.
因此,如果用户打开应用程序,他/她可以绘制形状,并且只能看到绘制的形状.
So, if a user opens up the application, he/she can draw shapes, and only see the shapes drawn.
我是 pyside 的新手,但我发现了只有小部件可见的示例,但我认为这不适用于此问题.
I'm new to pyside, but I've found examples where only the widget is visible, but I don't think that applies to this problem.
a = QPoint(22, 22)
b = QPoint(444, 444)
def __init__(self, parent=None):
QWidget.__init__(self, parent)
self.setGeometry(300, 300, 350, 350)
self.setWindowTitle('Draw circles')
def paintEvent(self, event):
paint = QPainter()
paint.begin(self)
paint.drawLine(self.a, self.b)
paint.end()
上面的程序只是在不透明背景上画一条线的例子.不知道如何从这个变成透明背景.
The above program is just an example of a line drawn on an opaque background. Not sure how to go from this to a transparent background.
推荐答案
您必须启用标志 Qt::WA_TranslucentBackground
:
from PySide import QtCore, QtGui
class Widget(QtGui.QWidget):
def __init__(self, parent=None):
super(Widget, self).__init__(parent)
self.setAttribute(QtCore.Qt.WA_TranslucentBackground, True)
def paintEvent(self, event):
a = QtCore.QPoint(22, 22)
b = QtCore.QPoint(444, 444)
painter = QtGui.QPainter(self)
pen = QtGui.QPen(QtGui.QColor("red"))
pen.setWidth(5)
painter.setPen(pen)
painter.drawLine(a, b)
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
w = Widget()
w.resize(640, 480)
w.show()
sys.exit(app.exec_())
正如它所说的 @Heike 似乎在 windows 中需要添加:
As it says @Heike it seems that in windows it is necessary to add:
self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
这篇关于如何为绘制的对象(例如 drawLine)制作透明背景?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!