我在设计图形编辑器。作为绘图的场景,我需要完全使用QGraphicsScene。我实现了向场景中添加矩形和椭圆。然而,我无法理解特殊面板与形状的实现。使用PyQt5(此面板取自Paint)在Python中选择图形的绘制需要实现以下面板:形状选择面板1
还需要实现将图形从面板拖动到画布,大致如图所示:形状选择面板2
此自定义面板可以使用哪些小部件或按钮?也许有一个信息资源或类似小组的实施示例?
具有添加形状和界面图像的实现的程序代码:
from PyQt5.QtWidgets import * from PyQt5.QtGui import * from PyQt5.QtCore import * import sys # window class class Window(QMainWindow): def __init__(self): super().__init__() wid = QWidget() self.setCentralWidget(wid) # setting title self.setWindowTitle("Paint with PyQt5") # setting geometry to main window self.setGeometry(100, 100, 800, 600) # Defining a scene rect of 400x200, with it's origin at 0,0. # If we don't set this on creation, we can set it later with .setSceneRect self.scene = QGraphicsScene(0, 0, 400, 200) # Set all items as moveable and selectable. for item in self.scene.items(): item.setFlag(QGraphicsItem.ItemIsMovable) item.setFlag(QGraphicsItem.ItemIsSelectable) # Define our layout. vbox = QVBoxLayout() up = QPushButton("Rect") up.clicked.connect(self.add_rect) vbox.addWidget(up) down = QPushButton("Elips") down.clicked.connect(self.add_elips) vbox.addWidget(down) view = QGraphicsView(self.scene) view.setRenderHint(QPainter.Antialiasing) hbox = QHBoxLayout(self) hbox.addLayout(vbox) hbox.addWidget(view) wid.setLayout(hbox) def add_rect(self): rect = QGraphicsRectItem(0, 0, 200, 50) rect.setPos(50, 20) rect.setFlag(QGraphicsItem.ItemIsMovable) rect.setFlag(QGraphicsItem.ItemIsSelectable) self.scene.addItem(rect) def add_elips(self): ellipse = QGraphicsEllipseItem(0, 0, 100, 100) ellipse.setPos(75, 30) ellipse.setFlag(QGraphicsItem.ItemIsMovable) ellipse.setFlag(QGraphicsItem.ItemIsSelectable) self.scene.addItem(ellipse) # create pyqt5 app App = QApplication(sys.argv) # create the instance of our Window window = Window() # showing the window window.show() # start the app sys.exit(App.exec())
界面图像:界面图像