Graphics View提供了一個平面,用於管理和交互大量自定義的2D圖形圖元,以及一個用於可視化圖元的視圖窗口小部件,支持縮放和旋轉。
該框架包括一個事件傳播架構,允許場景中圖元的精確雙精度交互功能。圖元可以處理關鍵事件,鼠標按下,移動,釋放和雙擊事件,還可以跟蹤鼠標移動。
Graphics View使用BSP(二進制空間分區)樹來提供非常快速的圖元發現,因此,即使有數百萬個圖元,它也可以實時顯示大型場景。
先說一下這里面的坐標系:
圖形視圖基於笛卡爾坐標系(平面直角坐標系x、y軸); 場景中的圖元位置和幾何圖形由兩個數字組成:x坐標和y坐標。 當使用未轉換的視圖觀察場景時,場景上的一個單元由屏幕上的一個像素表示。
注意:不支持反轉的Y軸坐標系(y向上增長),因為圖形視圖使用Qt的坐標系,也就是說x軸向右,y軸向下。如下圖:
圖形視圖中有三個有效的坐標系:圖元坐標,場景坐標和視圖坐標。 為了簡化您的實現,Graphics View提供了便利功能,允許您在三個坐標系之間進行映射。
渲染時,Graphics View的場景坐標對應於QPainter的邏輯坐標,視圖坐標與設備坐標相同。
圖元坐標(Item Coordinates)
圖元存在於他們自己的本地坐標系中。它們的坐標通常以其中心點(0,0)為中心,這也是所有變換的中心,如下圖:
圖元坐標系中的幾何圖元通常稱為圖元點,圖元線或圖元矩形。
創建自定義圖元時,您需要考慮圖元坐標; QGraphicsScene和QGraphicsView將為您執行所有轉換。這使得實現自定義圖元變得非常容易。例如,如果您收到鼠標按下或拖動輸入事件,則事件位置以圖元坐標給出。 QGraphicsItem.contains()虛函數,如果某個點在您的圖元內,則返回True,否則返回False,在圖元坐標中獲取一個點參數。類似地,圖元的邊界矩形和形狀在圖元坐標中。
在圖元的位置是圖元中心點在其父坐標系中的坐標;有時也稱為父坐標。在這個意義上,場景被視為所有無父圖元的“父母”。頂級圖元的位置在場景坐標中。
子坐標是相對於父坐標的。如果子圖元未轉換,子坐標和父坐標之間的差異與父坐標中圖元之間的距離相同。例如:如果未轉換的子圖元精確定位在其父項的中心點,則兩個圖元的坐標系統將完全相同。但是,如果孩子的位置是(10,0),則孩子的(0,10)點將對應於其父坐標的(10,10)點。
由於圖元的位置和變換是相對於父項的,因此子項的坐標不受父項轉換的影響,盡管父項的轉換會隱式轉換子項。即使父項被旋轉和縮放,子項(0,10)點仍將對應於父項(10,10)點。然而,相對於場景,孩子將遵循父母的轉變和位置。如果縮放父級(2x,2x),則子級的位置將位於場景坐標(20,0),並且其(10,0)點將對應於場景上的點(40,0)。
由於QGraphicsItem.pos()是少數例外之一,QGraphicsItem的函數在項坐標中運行,無論圖元或其父項的任何轉換如何。例如,圖元的邊界矩形(即QGraphicsItem.boundingRect())總是在圖元坐標中給出。
場景坐標(Scene Coordinates)
場景表示其所有圖元的基本坐標系。場景坐標系描述每個頂級圖元的位置,並且還形成從視圖傳遞到場景的所有場景事件的基礎。除了本地圖元pos和邊界矩形之外,場景中的每個圖元都有一個圖元位置和邊界矩形(QGraphicsItem.scenePos(),QGraphicsItem. sceneBoundingRect())。場景位置描述了圖元在場景坐標中的位置,其場景邊界矩形構成了QGraphicsScene如何確定場景的哪些區域已經改變的基礎。場景中的變化通過QGraphicsScene.changed()信號傳遞,參數是場景矩形列表。
視圖坐標(View Coordinates)
視圖坐標是小部件的坐標。視圖坐標中的每個單元對應於一個像素。這個坐標系的特殊之處在於它相對於窗口小部件或視口,並且不受觀察場景的影響。 QGraphicsView視口的左上角始終為(0,0),右下角始終為(視口寬度,視口高度)。所有鼠標事件和拖放事件最初都作為視圖坐標接收,您需要將這些坐標映射到場景以便與圖元進行交互。
坐標映射(Coordinate Mapping)
通常在處理場景中的圖元時,將場景中的坐標和任意形狀映射到圖元,圖元之間或視圖到場景都很有用。例如,當您在QGraphicsView的視口中單擊鼠標時,可以通過調用QGraphicsView.mapToScene(),然后調用QGraphicsScene.itemAt()來詢問場景下光標下的圖元。如果您想知道圖元所在視口中的位置,可以在圖元上調用QGraphicsItem.mapToScene(),然后在視圖上調用QGraphicsView.mapFromScene()。最后,如果您使用想要查找視圖橢圓內的圖元,可以將QPainterPath傳遞給mapToScene(),然后將映射的路徑傳遞給QGraphicsScene.items()。
您可以通過調用QGraphicsItem.mapToScene()和QGraphicsItem.mapFromScene()來將坐標和形狀映射到圖元的場景中。您還可以通過調用QGraphicsItem.mapToParent()和QGraphicsItem.mapFromParent()或通過調用QGraphicsItem.mapToItem()和QGraphicsItem.mapFromItem()來調用圖元的父圖元。所有映射函數都可以映射點,矩形,多邊形和路徑。
視圖中提供了相同的映射函數,用於映射到場景和從場景映射。 QGraphicsView.mapFromScene()和QGraphicsView.mapToScene()。 要從視圖映射到圖元,首先映射到場景,然后從場景映射到圖元。
主要特點
縮放和旋轉
QGraphicsView支持與QPainter通過QGraphicsView.setMatrix()相同的仿射變換。 通過對視圖應用變換,您可以輕松添加對常用導航功能(如縮放和旋轉)的支持。
打印
Graphics View通過其渲染函數QGraphicsScene.render()和QGraphicsView.render()提供單行打印。這些函數提供相同的API:您可以通過將QPainter傳遞給任一渲染函數,讓場景或視圖將其內容的全部或部分渲染到任何繪圖設備中。
場景和視圖渲染功能之間的區別在於,一個在場景坐標中操作,另一個在視圖坐標中操作。QGraphicsScene.render()通常首選打印未轉換場景的整個片段,例如繪制幾何數據或打印文本文檔。另一方面,QGraphicsView.render()適用於截屏;它的默認行為是使用提供的畫家(painter)渲染視口的確切內容。
拖、放
因為QGraphicsView間接地繼承了QWidget,所以它已經提供了與QWidget提供的相同的拖放功能。 此外,為方便起見,Graphics View框架還為場景和每個圖元提供拖放支持。 當視圖收到拖動時,它會將拖放事件轉換為QGraphicsSceneDragDropEvent,然后將其轉發到場景中。 場景接管此事件的調度,並將其發送到接受丟棄的鼠標光標下的第一個圖元。
要從圖元開始拖動,請創建QDrag對象,將指針傳遞給開始拖動的窗口小部件。 許多視圖可以同時觀察圖元,但只有一個視圖可以開始拖動。 在大多數情況下,拖動是由於按下或移動鼠標而啟動的,因此在mousePressEvent()或mouseMoveEvent()中,您可以從事件中獲取原始窗口小部件指針。
要攔截場景的拖放事件,您需要在QGraphicsItem子類中重新實現QGraphicsScene.dragEnterEvent()以及您的特定場景所需的任何事件處理程序。您可以在QGraphicsScene的每個事件處理程序的文檔中閱讀有關拖放圖形視圖的更多信息。
圖元可以通過調用QGraphicsItem.setAcceptDrops()來啟用拖放支持。要處理傳入的拖動,請重新實現QGraphicsItem.dragEnterEvent(),QGraphicsItem.dragMoveEvent(),QGraphicsItem.dragLeaveEvent()和QGraphicsItem.dropEvent()。
光標和工具提示
與QWidget一樣,QGraphicsItem也支持光標(QGraphicsItem.setCursor())和工具提示(QGraphicsItem.setToolTip())。當鼠標光標進入圖元區域時(通過調用QGraphicsItem.contains()檢測到),QGraphicsView將激活光標和工具提示。
您還可以通過調用QGraphicsView.setCursor()直接在視圖上設置默認光標。
轉載來自http://www.xdbcb8.com/archives/1621.html
*******************************************************************************************************************************************************************************************************************************************************
1:自定義圖元
class KEYTypeItem(QGraphicsItem): def __init__(self, PlanetType): super(KEYTypeItem, self).__init__() self.type = PlanetType def boundingRect(self): return QRectF(0, 0, 35, 35) def paint(self, painter, option, widget): painter.setPen(QColor(100,66,250)) painter.drawRect(0,0,30,20) painter.setPen(QColor(100, 166, 250)) painter.setBrush(Qt.black) painter.drawRect(5, 5, 20, 10) painter.setPen(QColor(245, 12, 231)) painter.drawText(4,-10,"%s"%self.type) ''' .drawPie(0,0,95,95,0*16,120*16)繪制扇形 .drawArc(0,0,95,95,30*16,120*16)繪制圓弧 .drawText(50,50,"文字")繪制文本 .drawRect(0,0,95,95)繪制矩形 .drawLine(0,0,0,95) 繪制直線 .drawEllipse(0, 0, 95, 95)繪制橢圓'''
這里我是自定義了一個外形像按鍵的類。
第一個方法就是初始化函數,不用多講。
第二個方法:
這個純虛函數將圖元的外邊界定義為矩形; 所有繪畫必須限制在圖元的邊界矩形內。 QGraphicsView使用它來確定圖元是否需要重繪。
盡管圖元的形狀可以是任意的,但是邊界矩形始終是矩形的,並且它不受圖元轉換的影響。
第三個方法:
這個方法使用qpainter來繪制我們圖元的圖案。具體每行代碼的作用,可以自己修改一下,看看效果就懂了,都非常簡單。
這里要注意的就是,里面涉及的坐標,指的是圖元坐標系。
2.創建場景,然后將圖元添加進去:
A:這條代碼創建了場景:self.graphicsView.scene = QtWidgets.QGraphicsScene(0, 0, 1024, 768)
B:這個方法就是建立圖元實體,然后將圖元添加到場景當中去。注意這里涉及的坐標是場景坐標系。
def createWireTypeItem(self):
item=WireypeItem()
item.setPos(55,300)
self.scene.addItem(item)
C:這條代碼就是將場景地址添加到視圖里面去,這樣我們的視圖就能看到場景當中的圖元了。
self.graphicsView.setScene(self.graphicsView.scene)
這里要注意的是:
我只有采用這種方法,將qtdesigner里面生成的mainwindow代碼帶入,代碼運行才沒有問題,這個我也不知道原因。不然會有000005錯誤。
class my_mainwindow(QtWidgets.QMainWindow, et.Ui_MainWindow): def __init__(self): super().__init__() self.setupUi(self) self.afterGenerationConfig() def afterGenerationConfig(self): self.graphicsView.scene = QtWidgets.QGraphicsScene(0, 0, 1024, 768) self.orb=OrbitalSimulation(self.graphicsView.scene) self.graphicsView.setScene(self.graphicsView.scene) self.graphicsView.setAlignment(QtCore.Qt.AlignLeft | QtCore.Qt.AlignTop) self.graphicsView.setSceneRect(0, 0, 1024, 768) # fix scene size 500 500 self.graphicsView.setRenderHint(QPainter.Antialiasing) ##設置視圖的抗鋸齒渲染模式。 ********************************************************** if __name__ == "__main__": app = QApplication(sys.argv) myApp = my_mainwindow() myApp.show() sys.exit(app.exec_())
**************************************************************************************************************************************************************************************
完整代碼:
qtdesigner生成代碼:
# -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'et.ui' # # Created by: PyQt5 UI code generator 5.11.2 # # WARNING! All changes made in this file will be lost! from PyQt5 import QtCore, QtGui, QtWidgets class Ui_MainWindow(object): def setupUi(self, MainWindow): MainWindow.setObjectName("MainWindow") MainWindow.resize(1281, 871) self.centralwidget = QtWidgets.QWidget(MainWindow) self.centralwidget.setObjectName("centralwidget") self.gridLayout = QtWidgets.QGridLayout(self.centralwidget) self.gridLayout.setObjectName("gridLayout") self.comboBox = QtWidgets.QComboBox(self.centralwidget) self.comboBox.setObjectName("comboBox") self.gridLayout.addWidget(self.comboBox, 0, 0, 1, 1) self.pushButton = QtWidgets.QPushButton(self.centralwidget) self.pushButton.setObjectName("pushButton") self.gridLayout.addWidget(self.pushButton, 0, 1, 1, 1) self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget) self.pushButton_2.setObjectName("pushButton_2") self.gridLayout.addWidget(self.pushButton_2, 1, 1, 1, 1) self.graphicsView = QtWidgets.QGraphicsView(self.centralwidget) self.graphicsView.setObjectName("graphicsView") self.gridLayout.addWidget(self.graphicsView, 0, 3, 3, 1) self.comboBox_2 = QtWidgets.QComboBox(self.centralwidget) self.comboBox_2.setObjectName("comboBox_2") self.gridLayout.addWidget(self.comboBox_2, 1, 0, 1, 1) MainWindow.setCentralWidget(self.centralwidget) self.menubar = QtWidgets.QMenuBar(MainWindow) self.menubar.setGeometry(QtCore.QRect(0, 0, 1281, 23)) self.menubar.setObjectName("menubar") MainWindow.setMenuBar(self.menubar) self.statusbar = QtWidgets.QStatusBar(MainWindow) self.statusbar.setObjectName("statusbar") MainWindow.setStatusBar(self.statusbar) self.retranslateUi(MainWindow) QtCore.QMetaObject.connectSlotsByName(MainWindow) def retranslateUi(self, MainWindow): _translate = QtCore.QCoreApplication.translate MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow")) self.pushButton.setText(_translate("MainWindow", "確定")) self.pushButton_2.setText(_translate("MainWindow", "確定"))
主代碼:
import et from PyQt5 import QtCore, QtWidgets from PyQt5.QtWidgets import QApplication,QMainWindow,QMessageBox import sys import os from PyQt5.QtWidgets import (QGraphicsItem, QGraphicsObject, QGraphicsScene, QGraphicsView) from PyQt5.QtGui import (QBrush, QColor, QDrag, QImage, QPainter, QPen, QPixmap, QPainterPath) from PyQt5.QtCore import (QEasingCurve, QFileInfo, QLineF, QMimeData, QPoint, QPointF, QPropertyAnimation, QRectF, Qt) mylist_16=[['XXX_IC','I','II','III','IV','V','VI','VII','VIII','IX', 'X', 'XI', 'XII', 'XIII', 'XIV', 'XV', 'XVI'], ['74LS138','A', 'B', 'B', 'G2A', 'G2B', 'G1', 'Y7', 'GND', 'Y6', 'Y5', 'Y4', 'Y3', 'Y2', 'Y1', 'Y0', 'VCC'], ['74LS161', '*R', 'CP', 'P0', 'P1', 'P2', 'P3', 'CEP', 'GND', 'PE-', 'CET', 'Q3', 'Q2', 'Q1', 'Q0', 'TC', 'VCC']] mylist_14=[['XXX_IC','I','II','III','IV','V','VI','VII','VIII','IX', 'X', 'XI', 'XII', 'XIII', 'XIV'], ['74LS00','1A', '1B', '1Y', '2A', '2B', '2Y', 'GND', '3Y', '3A', '3B', '4Y', '4A', '4B', 'VCC']] class my_mainwindow(QtWidgets.QMainWindow, et.Ui_MainWindow): def __init__(self): super().__init__() self.setupUi(self) self.name_list_16=['XXX_IC','74LS138','74LS161'] self.name_list_14 = ['XXX_IC', '74LS00'] self.comboBox.addItems(self.name_list_16) self.comboBox_2.addItems(self.name_list_14) self.afterGenerationConfig() self.pushButton.clicked.connect(self.task_pushbutton) self.pushButton_2.clicked.connect(self.task_pushbutton_2) def afterGenerationConfig(self): self.graphicsView.scene = QtWidgets.QGraphicsScene(0, 0, 1024, 768) self.orb=OrbitalSimulation(self.graphicsView.scene) self.graphicsView.setScene(self.graphicsView.scene) self.graphicsView.setAlignment(QtCore.Qt.AlignLeft | QtCore.Qt.AlignTop) self.graphicsView.setSceneRect(0, 0, 1024, 768) # fix scene size 500 500 self.graphicsView.setRenderHint(QPainter.Antialiasing) ##設置視圖的抗鋸齒渲染模式。 def task_pushbutton(self): name=self.comboBox.currentText() self.graphicsView.scene.removeItem(self.orb.item1) del self.orb.item1 self.orb.createIC16TypeItem(name) def task_pushbutton_2(self): name=self.comboBox_2.currentText() self.graphicsView.scene.removeItem(self.orb.item2) del self.orb.item2 self.orb.createIC14TypeItem(name) class IC16TypeItem(QGraphicsItem): def __init__(self, PlanetType): super(IC16TypeItem, self).__init__() self.type = PlanetType self.sequence=0 for i in range(3): if self.type==mylist_16[i][0]: self.sequence=i def boundingRect(self):##這個純虛函數將圖元的外邊界定義為矩形; 所有繪畫必須限制在圖元的邊界矩形內。 return QRectF(-10, -20, 250, 130) def paint(self, painter, option, widget): painter.setPen(QColor(166,66,250))##.NoPen) painter.setBrush(Qt.red)##.darkGray) for i in range(8): point1=30*i+0 painter.drawEllipse(point1, 80, 10, 10) painter.drawText(point1+2, 110, "%d"%(i+1)) painter.drawText(point1 + 2, 75, "%s" % (mylist_16[self.sequence][i+1])) for i in range(8): point1 = 30 * i + 0 painter.drawEllipse(point1, 0, 10, 10) painter.drawText(point1 + 2, -10, "%d" % (16-i)) painter.drawText(point1 + 2, 23, "%s" % (mylist_16[self.sequence][16-i])) painter.setPen(QColor(1, 66, 250)) ##.NoPen) painter.setBrush(Qt.NoBrush) painter.drawRect(-5,-5,240,100) painter.drawArc(-15,35,20,20,-90*16,180*16) painter.setPen(QColor(200, 166, 250)) painter.drawText(80, 50, "%s"%mylist_16[self.sequence][0]) ''' .drawPie(0,0,95,95,0*16,120*16)繪制扇形 .drawArc(0,0,95,95,30*16,120*16)繪制圓弧 .drawText(50,50,"文字")繪制文本 .drawRect(0,0,95,95)繪制矩形 .drawLine(0,0,0,95) 繪制直線 .drawEllipse(0, 0, 95, 95)繪制橢圓''' class IC14TypeItem(QGraphicsItem): def __init__(self, PlanetType): super(IC14TypeItem, self).__init__() self.type = PlanetType self.sequence=0 for i in range(2): if self.type==mylist_14[i][0]: self.sequence=i def boundingRect(self):##這個純虛函數將圖元的外邊界定義為矩形; 所有繪畫必須限制在圖元的邊界矩形內。 return QRectF(-10, -20, 250, 130) def paint(self, painter, option, widget): painter.setPen(QColor(166,66,250))##.NoPen) painter.setBrush(Qt.red)##.darkGray) for i in range(7): point1=30*i+0 painter.drawEllipse(point1, 80, 10, 10) painter.drawText(point1+2, 110, "%d"%(i+1)) painter.drawText(point1 + 2, 75, "%s" % (mylist_14[self.sequence][i+1])) for i in range(7): point1 = 30 * i + 0 painter.drawEllipse(point1, 0, 10, 10) painter.drawText(point1 + 2, -10, "%d" % (14-i)) painter.drawText(point1 + 2, 23, "%s" % (mylist_14[self.sequence][14-i])) painter.setPen(QColor(1, 66, 250)) ##.NoPen) painter.setBrush(Qt.NoBrush) painter.drawRect(-5,-5,210,100) painter.drawArc(-15,35,20,20,-90*16,180*16) painter.setPen(QColor(200, 166, 250)) painter.drawText(80, 50, "%s"%mylist_14[self.sequence][0]) ''' .drawPie(0,0,95,95,0*16,120*16)繪制扇形 .drawArc(0,0,95,95,30*16,120*16)繪制圓弧 .drawText(50,50,"文字")繪制文本 .drawRect(0,0,95,95)繪制矩形 .drawLine(0,0,0,95) 繪制直線 .drawEllipse(0, 0, 95, 95)繪制橢圓''' class CoordTypeItem(QGraphicsItem): def __init__(self, PlanetType="sun"): super(CoordTypeItem, self).__init__() self.type = PlanetType def boundingRect(self): return QRectF(0, 0, 1000, 750) def paint(self, painter, option, widget): painter.setPen(QColor(1,66,250)) painter.drawLine(0,0,0,700) painter.drawLine(0, 0, 800, 0) painter.drawLine(800, 0, 800, 700) painter.drawLine(0, 700, 800, 700) painter.setPen(QColor(100, 200, 3)) painter.drawText(700,120,"led燈") painter.drawText(700, 350, "芯片插槽") painter.drawText(700, 515, "按鍵") ''' .drawPie(0,0,95,95,0*16,120*16)繪制扇形 .drawArc(0,0,95,95,30*16,120*16)繪制圓弧 .drawText(50,50,"文字")繪制文本 .drawRect(0,0,95,95)繪制矩形 .drawLine(0,0,0,95) 繪制直線 .drawEllipse(0, 0, 95, 95)繪制橢圓 ''' class LEDTypeItem(QGraphicsItem): def __init__(self, PlanetType): super(LEDTypeItem, self).__init__() self.type = PlanetType def boundingRect(self): return QRectF(0, 0, 100, 100) def paint(self, painter, option, widget): painter.setPen(QColor(100,66,250)) painter.setBrush(Qt.red) painter.drawEllipse(0, 0, 20, 20) painter.drawLine(2, 5, 17, 17) painter.drawLine(2, 17, 17, 5) painter.setPen(QColor(245, 12, 231)) painter.drawText(0,-10,"%s"%self.type) ''' .drawPie(0,0,95,95,0*16,120*16)繪制扇形 .drawArc(0,0,95,95,30*16,120*16)繪制圓弧 .drawText(50,50,"文字")繪制文本 .drawRect(0,0,95,95)繪制矩形 .drawLine(0,0,0,95) 繪制直線 .drawEllipse(0, 0, 95, 95)繪制橢圓 ''' class KEYTypeItem(QGraphicsItem): def __init__(self, PlanetType): super(KEYTypeItem, self).__init__() self.type = PlanetType def boundingRect(self): return QRectF(0, 0, 35, 35) def paint(self, painter, option, widget): painter.setPen(QColor(100,66,250)) painter.drawRect(0,0,30,20) painter.setPen(QColor(100, 166, 250)) painter.setBrush(Qt.black) painter.drawRect(5, 5, 20, 10) painter.setPen(QColor(245, 12, 231)) painter.drawText(4,-10,"%s"%self.type) ''' .drawPie(0,0,95,95,0*16,120*16)繪制扇形 .drawArc(0,0,95,95,30*16,120*16)繪制圓弧 .drawText(50,50,"文字")繪制文本 .drawRect(0,0,95,95)繪制矩形 .drawLine(0,0,0,95) 繪制直線 .drawEllipse(0, 0, 95, 95)繪制橢圓''' class WireypeItem(QGraphicsItem): def __init__(self, PlanetType="sun"): super(WireypeItem, self).__init__() self.type = PlanetType def boundingRect(self): return QRectF(0, 0, 35, 35) def paint(self, painter, option, widget): #painter.setPen(QColor(250,66,250)) painter.setPen(Qt.DashLine) # QColor(1,66,250) painter.drawLine(0,0,0,-195) painter.drawLine(0, -195, 150, -195) ''' .drawPie(0,0,95,95,0*16,120*16)繪制扇形 .drawArc(0,0,95,95,30*16,120*16)繪制圓弧 .drawText(50,50,"文字")繪制文本 .drawRect(0,0,95,95)繪制矩形 .drawLine(0,0,0,95) 繪制直線 .drawEllipse(0, 0, 95, 95)繪制橢圓''' class OrbitalSimulation(): def __init__(self, scene): self.scene = scene self.createIC16TypeItem('74LS138') self.createIC14TypeItem('74LS00') self.createLEDTypeItem() self.createKEYTypeItem() self.createWireTypeItem() ##坐標系 self.item=CoordTypeItem() self.item.setPos(0,0) self.scene.addItem(self.item) def createIC16TypeItem(self,name): self.item1 = IC16TypeItem("%s"%name) self.item1.setPos(50, 300) self.scene.addItem(self.item1) def createIC14TypeItem(self,name): self.item2 = IC14TypeItem("%s"%name) self.item2.setPos(350, 300) self.scene.addItem(self.item2) def createLEDTypeItem(self): for i in range(8): ##range(2)=[0 1] item = LEDTypeItem('LED%d'%i)## if i else PointTypeItem("sun") point1 = 40 * i +200.0 item.setPos(point1,100) ##舉例:setPos(50,50)是把(圖元坐標)item坐標點(0,0)設置為與(場景坐標)scene坐標點(50,50)重合 self.scene.addItem(item) def createKEYTypeItem(self): for i in range(8): ##range(2)=[0 1] item=KEYTypeItem('KEY%d'%i) #item.setFlag(QGraphicsItem.ItemIsMovable) point1 = 50 * i + 200.0 item.setPos(point1,500) self.scene.addItem(item) def createWireTypeItem(self): item=WireypeItem() item.setPos(55,300) self.scene.addItem(item) if __name__ == "__main__": app = QApplication(sys.argv) myApp = my_mainwindow() myApp.show() sys.exit(app.exec_())