Vlad
Vlad

Reputation: 419

How to paint in colors QtGui.QPainterPath.addRect()?

I am trying to paint rect in PyQt5. But something always is not working with me. I was referring to "QPainterPath documentation' and there was this example :

path = QPainterPath()
path.addRect(20, 20, 60, 60)

path.moveTo(0, 0)
path.cubicTo(99, 0,  50, 50,  99, 99)
path.cubicTo(0, 99,  50, 50,  0, 0)

QPainter painter(self)
painter.fillRect(0, 0, 100, 100, Qt.white)
painter.setPen(QPen(QColor(79, 106, 25), 1, Qt.SolidLine,
                    Qt.FlatCap, Qt.MiterJoin))
painter.setBrush(QColor(122, 163, 39))

painter.drawPath(path)

I tried myself , but I can't under stand what is "QPainter painter(self)" and how it works there, I couldn't find QPainter command. Here is my example of code :

from PyQt5 import QtGui, QtCore, QtWidgets
import sys
class testUi(QtWidgets.QDialog):
    def __init__(self, parent=None):
        super(testUi, self).__init__(parent)
        self.window = 'vl_test'
        self.title = 'Test Remastered'
        self.size = (1000, 650)

        self.create( )

    def create(self):
        self.setWindowTitle(self.title)
        self.resize(QtCore.QSize(*self.size))
        self.testik = test(self)

        self.mainLayout = QtWidgets.QVBoxLayout( )
        self.mainLayout.addWidget(self.testik)
        self.setLayout(self.mainLayout)


class test(QtWidgets.QGraphicsView):
    def __init__(self, parent=None):
        super(test, self).__init__(parent)
        self._scene = QtWidgets.QGraphicsScene( )
        self.setScene(self._scene)
        self.drawSomething( )

    def drawSomething(self):
        self.path = QtGui.QPainterPath( )
        self.path.moveTo(0, 0)
        self.path.addRect(20, 20, 60, 60)

        self._scene.addPath(self.path)

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    v = testUi()
    v.show()
    sys.exit(app.exec_())

I didn't write anything after "addRect" in my code. So can anybody please clear how to use that "QPainter". Thank you for your time.

P.S Bonus question : What is fastest way to draw and interact with primitives in QGraphicScene? Because I can see there a lot of ways to draw curves/polygons and manipulate them, but what is most efficient for workflow ? Sorry for my english

Upvotes: 1

Views: 708

Answers (1)

eyllanesc
eyllanesc

Reputation: 244282

The addPath() method returns a QGraphicsPathItem that inherits from QAbstractGraphicsShapeItem that allows you to set the fill and border color with setBrush() and setPen(), respectively

def drawSomething(self):
    self.path = QtGui.QPainterPath()
    self.path.moveTo(0, 0)
    self.path.addRect(20, 20, 60, 60)
    item = self._scene.addPath(self.path)
    # or
    # item = QtWidgets.QGraphicsPathItem(path)
    # self._scene.addItem(item)
    item.setPen(
        QtGui.QPen(
            QtGui.QColor(79, 106, 25),
            1,
            QtCore.Qt.SolidLine,
            QtCore.Qt.FlatCap,
            QtCore.Qt.MiterJoin,
        )
    )
    item.setBrush(QtGui.QColor(122, 163, 39))

What is fastest way to draw and interact with primitives in QGraphicScene? Because I can there is a lot of ways to draw curves/polygons and manipulate them, but what is going to be most efficient for workflow?

The entire painting system in Qt ultimately uses QPainter, although the code I showed earlier does not see the explicit use but it is being used since QGraphicsItem has a paint that uses the QPainter, the brush and the pen.

Also QPainter is optimized so do not worry about the optimization from the Qt side.

QGraphicsView and QGraphicsScene is a high level framework that is based on the QGraphicsItems so in general you should be able to build anything with the predefined items, but if you can not then create your own QGraphicsItem (implementing the method paint() and boundingRect())

If you want to paint something permanent with which you do not want to interact (move it, click it, etc) then you can use the drawBackground or drawForeGround method of QGraphicsScene as you want to paint before or after the items are painted, respectively

For more information read: Graphics View Framework

Upvotes: 2

Related Questions