Skip to content Skip to sidebar Skip to footer

How To Create A Basic Custom Qgraphicseffect In Qt?

I have been trying to create a basic QGraphicsEffect to change the colors of the widget, but first I tried to make an effect that does nothing like so: class QGraphicsSepiaEffect(Q

Solution 1:

As your question is basically how to create a custom effect then based on an example offered by the Qt community I have translated it to PySide2:

import random
import sys

from PySide2 import QtCore, QtGui, QtWidgets
# or# from PyQt5 import QtCore, QtGui, QtWidgetsclassHighlightEffect(QtWidgets.QGraphicsEffect):
    def__init__(self, offset=1.5, parent=None):
        super(HighlightEffect, self).__init__(parent)
        self._color = QtGui.QColor(255, 255, 0, 128)
        self._offset = offset * QtCore.QPointF(1, 1)

    @propertydefoffset(self):
        return self._offset

    @propertydefcolor(self):
        return self._color

    @color.setterdefcolor(self, color):
        self._color = color

    defboundingRectFor(self, sourceRect):
        return sourceRect.adjusted(
            -self.offset.x(), -self.offset.y(), self.offset.x(), self.offset.y()
        )

    defdraw(self, painter):
        offset = QtCore.QPoint()
        try:
            pixmap = self.sourcePixmap(QtCore.Qt.LogicalCoordinates, offset)
        except TypeError:
            pixmap, offset = self.sourcePixmap(QtCore.Qt.LogicalCoordinates)

        bound = self.boundingRectFor(QtCore.QRectF(pixmap.rect()))
        painter.save()
        painter.setPen(QtCore.Qt.NoPen)
        painter.setBrush(self.color)
        p = QtCore.QPointF(offset.x() - self.offset.x(), offset.y() - self.offset.y())
        bound.moveTopLeft(p)
        painter.drawRoundedRect(bound, 5, 5, QtCore.Qt.RelativeSize)
        painter.drawPixmap(offset, pixmap)
        painter.restore()


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    w = QtWidgets.QWidget()
    lay = QtWidgets.QVBoxLayout(w)
    for _ inrange(3):
        o = QtWidgets.QLabel()
        o.setStyleSheet(
            """background-color : {}""".format(
                QtGui.QColor(*random.sample(range(255), 3)).name()
            )
        )
        effect = HighlightEffect(parent=o)
        o.setGraphicsEffect(effect)
        lay.addWidget(o)
    w.show()
    w.resize(640, 480)
    sys.exit(app.exec_())

enter image description here

Post a Comment for "How To Create A Basic Custom Qgraphicseffect In Qt?"