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