Skip to content

How to implement a shape selection panel for rendering in PyQt5? #2

Open
@Aleksandr34nov

Description

@Aleksandr34nov

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:

HDbv49RvRDI

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 and the interface image:
`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())`

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions