How to implement a shape selection panel for rendering in PyQt5?
Aleksandr34nov opened this issue · 8 comments
Environment : / 环境
- OS: [e.g. Win 10]
- Python [e.g. 3.6.4 X64]
- PyQt5 [e.g. 5.10.1]
I'm designing graphic editor. As a scene for drawing, I need to use exactly QGraphicsScene. I implemented adding rectangles and ellipses to the scene with two buttons.
I need to implement dragging the faigures from the panel to the canvas, approximately as in the picture:
What widgets or buttons can I use for this custom panel? Maybe there is an information an example with the implementation of a similar panel?
The program code with the implementation of adding shapes:
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())here is a good project to study
Sorry, I'm not very clear about your description.
if you want like this:
add this codeview.setDragMode(QGraphicsView.RubberBandDrag)
I'm sorry I didn't make myself clear enough.
I need a menu bar that is located outside the scene from where I can take a shape and drag it onto the scene by mouse. As in the Paint program or drawio. Menu with shapes in the graphic editor.

Sorry, I'm not very clear about your description. if you want like this: add this code
view.setDragMode(QGraphicsView.RubberBandDrag)
I try to explain again.
I am trying to implement a graphic editor. As a canvas for drawing, I use QGraphicsScene together with QGraphicsView.
There was a problem with the implementation of the panel, which is essentially a menu of shapes, from where it would be possible to drag shapes to QGraphicsView. It is located outside of QGraphicsView.
I would also like this menu to be divided into several sections so that they can be opened and closed by a button.
Is there an example of implementing this custom shapes menu? Or maybe thoughts with which it can be implemented?
This gif is the example of how I would like it to work (I use drawio as an example):

- for left panel, you can use
QTreeViewandQListViewicon model to show different icon. - you can see this example draggableicons to solve drag from left and drop on the right.
- when drop on the right, You can determine which type of graph is based on the
event.mimeData()type
I will write simple example latter
https://github.com/PyQt5/PyQt/blob/master/QGraphicsView/DragGraphics.py
like this?
Yes, this is what I need. Thank you very much.


