0

Hi every one I am working on using QPainter and mouse events and I want to set the background of the QPainter to black I used this code but doesn't work Thanks in advance

class Drawer(QWidget):

    newPoint = pyqtSignal(QPoint)
    def __init__(self, parent=None):
        QWidget.__init__(self, parent)
        self.path = QPainterPath()    

    def paintEvent(self, event):
        painter = QPainter(self)
        painter.setPen(QColor(168, 34, 3))
        painter.setFont(QFont('Decorative', 10))
        bbrush = QtGui.QBrush( QtGui.QColor(0,0,0),QtCore.Qt.SolidPattern)
        painter.setBackground(painter,bbrush)
        painter.drawPath(self.path)

    def mousePressEvent(self, event):
        self.path.moveTo(event.pos())
        self.update()

    def mouseMoveEvent(self, event):
        self.path.lineTo(event.pos())
        self.newPoint.emit(event.pos())
        self.update()

    def sizeHint(self):
        return QSize(400, 400)   

class MyWidget(QWidget):

    def __init__(self, parent=None):
        QWidget.__init__(self, parent)
        self.setLayout(QVBoxLayout())
        label = QLabel(self)
        drawer = Drawer(self)
        drawer.newPoint.connect(lambda p: label.setText(
                        'Coordinates: ( %d : %d )' % (p.x(), p.y())))
        self.layout().addWidget(label)
        self.layout().addWidget(drawer)
PRMoureu
  • 12,817
  • 6
  • 38
  • 48
ou2105
  • 165
  • 3
  • 10
  • Change `painter.setBackground(painter,bbrush)` to `painter.setBackground(bbrush)` – eyllanesc Oct 16 '17 at 21:08
  • Thanks Man but still not working in fact I have the whole code like this : – ou2105 Oct 16 '17 at 21:22
  • I do not understand what you want to do, just comment because the setBackgound() method does not require a QPainter. I do not understand what you want, you could show an image of what you get and another of what you want. – eyllanesc Oct 16 '17 at 21:26
  • https://stackoverflow.com/questions/46633698/pyqt5-painting-using-events the result of the code that you already answread here I just want to set the QPainter background to black – ou2105 Oct 16 '17 at 21:42
  • I do not understand what you mean by background, you mean the bottom of the widget? – eyllanesc Oct 17 '17 at 03:44

1 Answers1

1

If you are just trying to get a black background you can get that without the paintEvent.

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *


class Drawer(QWidget):

    newPoint = pyqtSignal(QPoint)

    def __init__(self, parent=None):
        QWidget.__init__(self, parent)
        self.path = QPainterPath()    

        # set black background
        pal = self.palette()
        pal.setColor(QPalette.Background, Qt.black)
        self.setAutoFillBackground(True)
        self.setPalette(pal)

    def mousePressEvent(self, event):
        self.path.moveTo(event.pos())
        self.update()

    def mouseMoveEvent(self, event):
        self.path.lineTo(event.pos())
        self.newPoint.emit(event.pos())
        self.update()

    def sizeHint(self):
        return QSize(400, 400)


class MyWidget(QWidget):

    def __init__(self, parent=None):
        QWidget.__init__(self, parent)
        self.setLayout(QVBoxLayout())
        label = QLabel(self)
        drawer = Drawer(self)
        drawer.newPoint.connect(lambda p: label.setText(
                        'Coordinates: ( %d : %d )' % (p.x(), p.y())))
        self.layout().addWidget(label)
        self.layout().addWidget(drawer)


app = QApplication(sys.argv)
widget = MyWidget()
widget.show()

sys.exit(app.exec_())
shao.lo
  • 4,387
  • 2
  • 33
  • 47