Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Aleksandr34nov opened this issue Feb 7, 2023 · 0 comments

Comments

@Aleksandr34nov
Copy link

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())`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant