PyQt5 / PyQt

PyQt Examples(PyQt各种测试和例子) PyQt4 PyQt5
GNU Lesser General Public License v2.1
6.65k stars 1.97k forks source link

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

Closed Aleksandr34nov closed 1 year ago

Aleksandr34nov commented 1 year ago

Environment : / 环境

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:

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())
892768447 commented 1 year ago

Sorry, I'm not very clear about your description. if you want like this: add this code view.setDragMode(QGraphicsView.RubberBandDrag) 无标题

892768447 commented 1 year ago

here is a good project to study

https://github.com/jchanvfx/NodeGraphQt

Aleksandr34nov commented 1 year ago

Sorry, I'm not very clear about your description. if you want like this: add this code view.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. F67CAA19-2057-4A68-A45F-C598006DA794

Aleksandr34nov commented 1 year ago

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): Shape_menu

892768447 commented 1 year ago

@Aleksandr34nov

  1. for left panel, you can use QTreeView and QListView icon model to show different icon.
  2. you can see this example draggableicons to solve drag from left and drop on the right.
  3. when drop on the right, You can determine which type of graph is based on the event.mimeData() type
892768447 commented 1 year ago

I will write simple example latter

892768447 commented 1 year ago

https://github.com/PyQt5/PyQt/blob/master/QGraphicsView/DragGraphics.py like this? DragGraphics

Aleksandr34nov commented 1 year ago

https://github.com/PyQt5/PyQt/blob/master/QGraphicsView/DragGraphics.py like this? DragGraphics

Yes, this is what I need. Thank you very much.