how to make transparent background for drawn objects (ex. drawLine)?

耗尽温柔 提交于 2020-08-26 07:42:06

问题


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.

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.


回答1:


You must enable the flag 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_())

As it says @Heike it seems that in windows it is necessary to add:

self.setWindowFlags(QtCore.Qt.FramelessWindowHint) 


来源:https://stackoverflow.com/questions/56434458/how-to-make-transparent-background-for-drawn-objects-ex-drawline

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!