0

I want to know how to keep scene widgets after add them to scene for later. After user's click on marker button, marker feature active and with rightclick can mark points in QGraphicscene.

Here is my code:

from PyQt5.QtWidgets import QMainWindow, QToolButton, QGraphicsScene, QGraphicsPixmapItem
from PyQt5.QtGui import QPixmap, QIcon, QPainter
from PyQt5 import uic
from PyQt5.QtCore import Qt
class MarkerButton(QToolButton):
    def __init__(self, *args, **kwargs):
        super(MarkerButton, self).__init__(*args, **kwargs)

        

    def mousePressEvent(self, event):
        if event.buttons() & Qt.LeftButton:
            self.deleteLater()
            super().mousePressEvent(event)

class MarkerScene(QGraphicsScene):
    def __init__(self, *args, **kwargs):
        super(MarkerScene, self).__init__(*args, **kwargs)

        self.marker_widgets = dict()



    def mousePressEvent(self, event):
        if event.buttons() & Qt.RightButton:
          self.start_point = event.scenePos()
        
          self.update_path()
          super().mousePressEvent(event)

    def update_path(self):
        if  not self.start_point.isNull():
            pushButton_icon = QIcon()
            pushButton_icon.addPixmap(QPixmap('assets/mark.png')) 
            new_marker = MarkerButton()
            new_marker.setToolButtonStyle(Qt.ToolButtonTextUnderIcon)
            new_marker.move(int(self.start_point.x()), int(self.start_point.y()))
            new_marker.setIcon(pushButton_icon)
            new_marker.setText(str(len(self.marker_widgets)))
            self.addWidget(new_marker)
            self.marker_widgets[new_marker] = self.start_point




class Ui(QMainWindow):
    def __init__(self):
        super(Ui, self).__init__() # Call the inherited classes __init__ method
        
        self.Window_obj= uic.loadUi('main.ui') 
        self.Window_obj.setWindowFlags(Qt.FramelessWindowHint)
        
        pix = QPixmap('assets/data.png')
        item = QGraphicsPixmapItem(pix)
        scene = QGraphicsScene(self)
        scene.addItem(item)
        self.Window_obj.graphicsViewScene1.setScene(scene)


        self.Window_obj.show()
    def AddMarker(self):
        
        if not self.active_toolbox_markers:
            self.active_toolbox_markers = True
            self.main_Window_obj.graphicsViewScene1.setRenderHint(QPainter.Antialiasing)
            self.Window_obj.graphicsViewScene1.setMouseTracking(True)
            

            scene = MarkerScene()

         
            self.main_Window_obj.graphicsViewScene1.setScene(scene)

            

        else:
            self.active_toolbox_markers = False
        
        

I need to save this markers for later but user have to be able to hide them, the reason is because I have another tool that I want not conflict with this markers.

At the end, after add markers, the main Qpixmap removed. my app something like this

WhoKnowsMe
  • 498
  • 2
  • 13
md.119
  • 29
  • 7

0 Answers0