In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
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!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.