您好,登錄后才能下訂單哦!
這篇文章主要講解了“pyqt5怎么使用QGraphicsScene及QGraphicsView”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“pyqt5怎么使用QGraphicsScene及QGraphicsView”吧!
效果圖:
from PyQt5.QtCore import Qt, QRectF from PyQt5.QtGui import QColor, QPen, QBrush, QFont from PyQt5.QtWidgets import (QGraphicsView, QGraphicsScene, QApplication) class MainWindow(QGraphicsView): def __init__(self, parent=None): super(MainWindow, self).__init__(parent) # 創(chuàng)建場景 self.scene = MyGraphScene(self) # 在場景中添加文字 self.addPoint(0, 0, "p1") self.addPoint(50, 100, "p2") self.addPoint(100, 0, "p3") self.setSceneRect(QRectF(-150, -150, 400, 400)) self.scale(2, 2) # 將場景加載到窗口 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): # 在這里可以繪制底板,比如網(wǎng)格 pass if __name__ == '__main__': import sys # 每個PyQt程序必須創(chuàng)建一個application對象,sys.argv 參數(shù)是命令行中的一組參數(shù) # 注意:application在 PyQt5.QtWidgets 模塊中 # 注意:application在 PyQt4.QtGui 模塊中 app = QApplication(sys.argv) # 創(chuàng)建桌面窗口 mainWindow = MainWindow() # 顯示桌面窗口 mainWindow.show() sys.exit(app.exec_())
使用概要:
1、創(chuàng)建繼承自QGraphicsView的窗口
2、創(chuàng)建繼承自QGraphicsScene的畫布
3、將畫布設(shè)置給View窗口QGraphicsView::setScene(self.scene)
4、自由的在畫布上添加元素:
①通過已經(jīng)封裝好的方法,如前面代碼使用的
②自定義item,繼承自QGraphicsItem該類,并通過QGraphicsScene::addItem(item)的方法將item添加到畫布
QGraphicsView的API
QGraphicsScene的API
感謝各位的閱讀,以上就是“pyqt5怎么使用QGraphicsScene及QGraphicsView”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對pyqt5怎么使用QGraphicsScene及QGraphicsView這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關(guān)知識點的文章,歡迎關(guān)注!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。