1

Как изменить цвет заливки с белого на какой-нибудь другой при изменении положения QSizeGrip?

Как убрать белую заливку ?

from PyQt5.QtCore    import (Qt, QRect)
from PyQt5.QtGui     import (QBrush, QColor, QPainter, QPixmap, QPen)
from PyQt5.QtWidgets import (QApplication, QGraphicsView, QGraphicsScene,
                         QLabel, QSizeGrip)


class QSizeGripAdvanced(QSizeGrip):
    def __init__(self, parent):
        super().__init__(parent)

    def paintEvent(self, e):
        print('ps')


class Holst(QLabel):
    def __init__(self):
        super().__init__()
        self.setAcceptDrops(True)
        self.flag = False

        self.pixmap = QPixmap('hold.png')
        self.setPixmap(self.pixmap)
        self.resize(self.pixmap.size())
        self.move(60, 130)

        self.toolName = ''
        self.widthPen = 10
        self.firstColor = QColor('#FFFFFF')
        self.secondColor = QColor('#FFFFFF')

        self.noColor = False

        self.firstTouch = False
        self.secondTouch = False
        self.x = 0
        self.y = 0
        self.firstX = 0
        self.firstY = 0


class DrawWidget(QGraphicsView):
    backgroundColor = QColor(0, 31, 34)
    def __init__(self):
        super().__init__()
        self.resize(40, 40)
        self.setBackgroundBrush(self.backgroundColor)
        self.setCacheMode(self.CacheBackground)
        self.setRenderHints(
            QPainter.Antialiasing | QPainter.TextAntialiasing | 
                QPainter.SmoothPixmapTransform)
        self._scene = QGraphicsScene()
        self.holst = Holst()
        self._scene.addWidget(self.holst)

        d = QSizeGripAdvanced(self.holst)
        d.move(self.holst.pixmap.width() + 100, self.holst.pixmap.height() + 50)
        d.resize(50, 50)
        self._scene.addWidget(d)

        self.setScene(self._scene)


import sys
app=QApplication(sys.argv)
w = DrawWidget()
w.show()
sys.exit(app.exec_())

из за этого следует это

1 ответ 1

2

Свойство styleSheet содержит таблицу стилей виджета.

from PyQt5.QtCore    import (Qt, QRect)
from PyQt5.QtGui     import (QBrush, QColor, QPainter, QPixmap, QPen)
from PyQt5.QtWidgets import (QApplication, QGraphicsView, QGraphicsScene,
                         QLabel, QSizeGrip)

class QSizeGripAdvanced(QSizeGrip):
    def __init__(self, parent):
        super().__init__(parent)
        self.parent = parent                             # +++

    def paintEvent(self, e):
        print('ps')

    def mouseMoveEvent(self, event):                     # +++
        super(QSizeGripAdvanced, self).mouseMoveEvent(event)
        if event.buttons() == Qt.LeftButton:
            self.setStyleSheet('background-color: rgba(235, 15, 55, 150);') # <-----
            self.move(self.parent.pos().x()+self.parent.width()-20, 
                      self.parent.pos().y()+self.parent.height()-20)

            self.parent.setStyleSheet('background-color: rgba(10, 15, 55, 150);') # <-----


class Holst(QLabel):
    def __init__(self):
        super().__init__()
        self.setAcceptDrops(True)
        self.flag = False

        self.pixmap = QPixmap('E:/_Qt/img/add.png')
        self.setPixmap(self.pixmap)
        self.resize(self.pixmap.size())
        self.move(60, 130)

        self.toolName = ''
        self.widthPen = 10
        self.firstColor  = QColor('#FFFFFF')
        self.secondColor = QColor('#FFFFFF')

        self.noColor = False

        self.firstTouch  = False
        self.secondTouch = False
        self.x = 0
        self.y = 0
        self.firstX = 0
        self.firstY = 0

class DrawWidget(QGraphicsView):

    backgroundColor = QColor(0, 31, 34)

    def __init__(self):
        super().__init__()

        self.resize(240, 240)
        self.setBackgroundBrush(self.backgroundColor)
        self.setCacheMode(self.CacheBackground)
        self.setRenderHints(
            QPainter.Antialiasing | QPainter.TextAntialiasing | 
                QPainter.SmoothPixmapTransform) 

        self._scene = QGraphicsScene()
        self.holst  = Holst()
        self._scene.addWidget(self.holst)

        d = QSizeGripAdvanced(self.holst)
        d.move(self.holst.pixmap.width() + 100, self.holst.pixmap.height() + 50)
        #d.resize(50, 50)
        self._scene.addWidget(d)
        self.setScene(self._scene)

if __name__ == "__main__":
    import sys
    app=QApplication(sys.argv)
    #app.setStyle('Fusion')
    w = DrawWidget()
    w.show()
    sys.exit(app.exec_())

введите сюда описание изображения

введите сюда описание изображения

2
  • извините за мою оплошность но я имел ввиду, когда меняем положение QSizeGrip цвет закраски фона с белого закрашивался другим цветом,
    – Daniil
    29 сен 2018 в 17:20
  • @Daniil я добавил строку )
    – S. Nick
    29 сен 2018 в 17:57

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.