Ejemplo n.º 1
0
class Example(QWidget):
    def __init__(self):
        super().__init__()

        self.initUI()

    def initUI(self):

        self.col = QColor(0, 0, 0)

        redb = QPushButton('Red', self)
        redb.setCheckable(True)
        redb.move(10, 10)

        redb.clicked[bool].connect(self.setColor)

        greenb = QPushButton('Green', self)
        greenb.setCheckable(True)
        greenb.move(10, 60)

        greenb.clicked[bool].connect(self.setColor)

        blueb = QPushButton('Blue', self)
        blueb.setCheckable(True)
        blueb.move(10, 110)

        blueb.clicked[bool].connect(self.setColor)

        self.square = QFrame(self)
        self.square.setGeometry(150, 20, 100, 100)
        self.square.setStyleSheet("QWidget { background-color: %s }" %
                                  self.col.name())

        self.setGeometry(300, 300, 300, 250)
        self.setWindowTitle('Toggle button')
        self.show()

    def setColor(self, pressed):

        source = self.sender()

        if pressed:
            val = 255
        else:
            val = 0

        if source.text() == "Red":
            self.col.setRed(val)
        elif source.text() == "Green":
            self.col.setGreen(val)
        else:
            self.col.setBlue(val)

        self.square.setStyleSheet("QFrame { background-color: %s }" %
                                  self.col.name())
Ejemplo n.º 2
0
class ColorImage(QQuickPaintedItem):
    def __init__(self, parent=None):
        super().__init__(parent)
        self._source = QUrl()
        self._color = QColor()
        self._svg_data = b""
        self._renderer = None

    sourceChanged = pyqtSignal()
    colorChanged = pyqtSignal()

    def _updateSVG(self) -> None:
        if not self._source or self._source.toLocalFile() == "":
            return
        try:
            with open(self._source.toLocalFile(), "rb") as f:
                self._svg_data = f.read()
        except FileNotFoundError:
            Logger.log(
                "w",
                f"Unable to find image located at {self._source.toLocalFile()}"
            )
            return
        self._svg_data = self._svg_data.replace(
            b"<svg ",
            b"<svg fill=\"%s\" " % self._color.name().encode("utf-8"))
        self._renderer = QSvgRenderer(self._svg_data)
        self.update()

    def setSource(self, source: QUrl) -> None:
        if self._source != source:
            self._source = source
            self.sourceChanged.emit()
            self._updateSVG()

    @pyqtProperty(QUrl, fset=setSource, notify=sourceChanged)
    def source(self) -> QUrl:
        return self._source

    def setColor(self, color: QColor) -> None:
        if self._color != color:
            self._color = color
            self.colorChanged.emit()
            self._updateSVG()

    @pyqtProperty(QColor, fset=setColor, notify=colorChanged)
    def color(self) -> QColor:
        return self._color

    def paint(self, painter: QPainter) -> None:
        pixel_ratio = Application.getInstance().getMainWindow(
        ).effectiveDevicePixelRatio()
        painter.scale(1 / pixel_ratio, 1 / pixel_ratio)
        if self._renderer:
            self._renderer.render(painter)
Ejemplo n.º 3
0
    def initUI(self):

        col = QColor(0, 0, 0)

        self.btn = QPushButton('Dialog', self)
        self.btn.move(20, 20)

        self.btn.clicked.connect(self.showDialog)

        self.frm = QFrame(self)
        self.frm.setStyleSheet("QWidget { background-color: %s }" % col.name())
        self.frm.setGeometry(130, 22, 200, 200)

        self.setGeometry(300, 300, 450, 350)
        self.setWindowTitle('Color dialog')
        self.show()