Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use QGraphicsScene and QGraphicsView in pyqt5

Shulou Source: shulou.com Published: 2022-06-02 21:42:36 10月15日 Update

This article mainly explains "how to use QGraphicsScene and QGraphicsView in pyqt5". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to use QGraphicsScene and QGraphicsView in pyqt5.

Effect picture:

From PyQt5.QtCore import Qt, QRectFfrom PyQt5.QtGui import QColor, QPen, QBrush, QFontfrom PyQt5.QtWidgets import (QGraphicsView, QGraphicsScene, QApplication) class MainWindow (QGraphicsView): def _ _ init__ (self, parent=None): super (MainWindow, self). _ _ init__ (parent) # create scene self.scene = MyGraphScene (self) # add text self.addPoint (0,0, "p1") self.addPoint (50) to the scene 100, "p2") self.addPoint (100,0, "p3") self.setSceneRect (QRectF (- 150,400,400) self.scale (2,2) # load the scene into the window self.setScene (self.scene) def addPoint (self, x, y, name): self.scene.addEllipse (x, y, 16, 16, QPen (QColor (Qt.red) QBrush (QColor (Qt.red)) text = self.scene.addText (name) text.setDefaultTextColor (QColor (Qt.red)) text.setFont (QFont ("Courier New", 16)) text.setPos (x, y-30) class MyGraphScene (QGraphicsScene): def _ init__ (self, parent=None): super (MyGraphScene, self). _ init__ (parent) def drawBackground (self, painter) Rect): # you can draw the backplane here For example, the grid passif _ _ name__ = ='_ _ main__': import sys # every PyQt program must create an application object The sys.argv parameter is a set of parameters in the command line # Note: application in the PyQt5.QtWidgets module # Note: application in the PyQt4.QtGui module app = QApplication (sys.argv) # create the desktop window mainWindow = MainWindow () # display the desktop window mainWindow.show () sys.exit (app.exec_ ())

Summary of usage:

1. Create a window inherited from QGraphicsView

2. Create a canvas inherited from QGraphicsScene

3. Set the canvas to View window QGraphicsView::setScene (self.scene)

4. Freely add elements to the canvas:

① uses methods that have been encapsulated, such as those used by the previous code

② custom item, which inherits from the QGraphicsItem class, and adds item to the canvas through the QGraphicsScene::addItem (item) method

API of QGraphicsView

API of QGraphicsScene

Thank you for your reading, the above is the content of "how pyqt5 uses QGraphicsScene and QGraphicsView". After the study of this article, I believe you have a deeper understanding of how pyqt5 uses QGraphicsScene and QGraphicsView, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Canvas scene learning content parameters methods desktops modules freedom code elements commands objects that is backplane ideas situations effects effect drawings text Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei NVidia Xiaomi Shulou Information Microsoft