1

I am trying to use PyQt (PySide6) to draw a Pipeline network like in this example gif [1]: https://i.stack.imgur.com/uQsRg.gif

I know i have to use the QGraphicsView class with QGraphicsScene to draw elements in the screen.

What i dont know how to do is how to handle all the mouse click and move events as well as having Ports on each side of the pipe to be able to attach other pipes/elements to pipes.

i also have to be able to double click on elements to configure them.

Is there any good documentation where i can learn how to achieve this ? or any tutorials ?

Thank you.

import sys
from PySide6.QtWidgets import (
    QApplication,
    QMainWindow,
    QGraphicsView,
    QGraphicsScene,
)
from PySide6.QtGui import QAction
from PySide6.QtCore import Qt


class GraphicsScene(QGraphicsScene):
    def __init__(self):
        super().__init__()

    def mousePressEvent(self, event) -> None:
        if event.button() == Qt.LeftButton:
            print("Left button pressed")
            pos_x = event.scenePos().x()
            pos_y = event.scenePos().y()
            print(f"Position: {pos_x}, {pos_y}")


class GraphicsView(QGraphicsView):
    def __init__(self):
        super().__init__()
        self.scene = self.setScene(GraphicsScene())


class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        """Set up the application's GUI."""
        self.setMinimumSize(450, 350)
        self.setWindowTitle("Main Window")
        self.setup_main_window()
        self.create_actions()
        self.create_menu()
        self.show()

    def setup_main_window(self):
        """Create and arrange widgets in the main window."""
        self.setCentralWidget(GraphicsView())

    def create_actions(self):
        """Create the application's menu actions."""
        # Create actions for File menu
        self.quit_act = QAction("&Quit")
        self.quit_act.setShortcut("Ctrl+Q")
        self.quit_act.triggered.connect(self.close)

    def create_menu(self):
        """Create the application's menu bar."""
        self.menuBar().setNativeMenuBar(False)
        # Create file menu and add actions
        file_menu = self.menuBar().addMenu("File")
        file_menu.addAction(self.quit_act)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MainWindow()
    sys.exit(app.exec())

EDIT/UPDATE :

Here's a solution. Thanks for the help

import sys
from PySide6.QtWidgets import (
    QApplication,
    QMainWindow,
    QGraphicsView,
    QGraphicsScene,
    QGraphicsEllipseItem,
)
from PySide6.QtGui import QPainterPath, QTransform, QPen, QBrush, QColor, QPainter
from PySide6.QtCore import Qt


PORT_PEN_COLOR = "#000000"
PORT_BRUSH_COLOR = "#ebebeb"
EDGE_PEN_COLOR = "#474747"


class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setGeometry(0, 0, 800, 600)
        self.setCentralWidget(GraphicsView())
        self.show()


class GraphicsView(QGraphicsView):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setMouseTracking(True)
        self.setScene(GraphicsScene())
        self.setRenderHint(QPainter.RenderHint.Antialiasing)


class GraphicsScene(QGraphicsScene):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setSceneRect(-10000, -10000, 20000, 20000)
        self._port_pen = QPen(QColor(PORT_PEN_COLOR))
        self._port_brush = QBrush(QColor(PORT_BRUSH_COLOR))
        self._edge_pen = QPen(QColor(EDGE_PEN_COLOR))
        self._edge_pen.setWidth(4)

    def mousePressEvent(self, event):
        clicked_item = self.itemAt(event.scenePos(), QTransform())
        if event.buttons() == Qt.MouseButton.LeftButton:
            if clicked_item is not None:
                # edge item
                pos = clicked_item.scenePos()
                pos.setX(pos.x() + 6)
                pos.setY(pos.y() + 6)
                self.edge = self.addPath(QPainterPath())
                self.edge.setPen(self._edge_pen)
                self.start_pos = pos
                self.end_pos = self.start_pos
                self.update_path()
            else:
                x = event.scenePos().x()
                y = event.scenePos().y()
                # port item
                start_port = Ellipse()
                start_port.setPos(x - 6, y - 6)
                start_port.setPen(self._port_pen)
                start_port.setBrush(self._port_brush)
                start_port.setZValue(10000.0)
                self.addItem(start_port)
                # edge item
                self.edge = self.addPath(QPainterPath())
                self.edge.setPen(self._edge_pen)
                self.start_pos = event.scenePos()
                self.end_pos = self.start_pos
                self.update_path()

    def mouseMoveEvent(self, event):
        if event.buttons() == Qt.MouseButton.LeftButton:
            print(f"moving, x : {event.scenePos().x()}, y : {event.scenePos().y()}")
            self.end_pos = event.scenePos()
            try:
                self.update_path()
            except AttributeError:
                pass

    def mouseReleaseEvent(self, event) -> None:
        released_item = self.itemAt(event.scenePos(), QTransform())
        if event.button() == Qt.MouseButton.LeftButton:
            if released_item is not None and released_item.type() != 2:
                self.end_pos = released_item.scenePos()
                self.end_pos.setX(self.end_pos.x() + 6)
                self.end_pos.setY(self.end_pos.y() + 6)
                if not self.start_pos.isNull() and not self.end_pos.isNull():
                    path = QPainterPath()
                    path.moveTo(self.start_pos.x() - 1, self.start_pos.y() - 1)
                    path.lineTo(self.end_pos)
                    self.edge.setPath(path)
            else:
                x = event.scenePos().x() + 1
                y = event.scenePos().y() + 1
                end_port = QGraphicsEllipseItem(0, 0, 10, 10)
                end_port.setPos(x - 6, y - 6)
                end_port.setPen(self._port_pen)
                end_port.setBrush(self._port_brush)
                end_port.setZValue(10000.0)
                self.addItem(end_port)

    def update_path(self):
        if not self.start_pos.isNull() and not self.end_pos.isNull():
            path = QPainterPath()
            path.moveTo(self.start_pos.x() - 1, self.start_pos.y() - 1)
            path.lineTo(self.end_pos)
            self.edge.setPath(path)


class Ellipse(QGraphicsEllipseItem):
    def __init__(self):
        super().__init__()
        self.setRect(0, 0, 10, 10)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    win = MainWindow()
    sys.exit(app.exec())
YacineCino
  • 33
  • 6
  • it may need to write all code on your own - check button, check if there is object in small distance, remeber this object, draw this object as selected (with some extra color), update object position when move mouse, redraw all objects, etc. So it may need some list to keep all objects, search if mouse is near of one of object on list, update objects on list, use list to redraw objects on screen (in new positions) – furas Jun 19 '22 at 11:40
  • Okay Thank you, i'll look up on how to code all these steps that mentioned. – YacineCino Jun 19 '22 at 12:53

2 Answers2

0

It may need to write all code on your own - check button, check if there is object in small distance, remeber this object, draw this object as selected (with some extra color), update object position when move mouse, redraw all objects, etc. So it may need some list to keep all objects, search if mouse is near of one of object on list, update objects on list, use list to redraw objects on screen (in new positions)


Minimal code which use left click to add item (rectangle), and right click to delete it.

EDIT:

I found out that scene has function .items() to access all items and I don't have to use own list objects for this.

import sys
from PySide6.QtWidgets import (
    QApplication,
    QMainWindow,
    QGraphicsView,
    QGraphicsScene,
    QGraphicsRectItem,
)
from PySide6.QtGui import QAction
from PySide6.QtCore import Qt

#objects = []

class GraphicsScene(QGraphicsScene):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setSceneRect(-100, -100, 200, 200)
        
    def mousePressEvent(self, event) -> None:
        if event.button() == Qt.LeftButton:
            #print("Left button pressed")
            x = event.scenePos().x()
            y = event.scenePos().y()
            #print(f"Position: {x}, {y}")

            rectitem = QGraphicsRectItem(0, 0, 10, 10)
            # set center of rectangle in mouse position
            rectitem.setPos(x-5, y-5)
            self.addItem(rectitem)

            #objects.append(rectitem)
            
        elif event.button() == Qt.RightButton:
            #print("Right button pressed")
            x = event.scenePos().x()
            y = event.scenePos().y()
            #print(f"Position: {x}, {y}")

            #for item in objects:
            for item in self.items():
                pos = item.pos() 
                if abs(x-pos.x()) < 10 and abs(y-pos.y()) < 10:
                    print('selected:', item)
                    self.removeItem(item)
                    #objects.remove(item)
                    break

                
class GraphicsView(QGraphicsView):
    def __init__(self):
        super().__init__()
        self.scene = self.setScene(GraphicsScene())


class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        """Set up the application's GUI."""
        self.setMinimumSize(450, 350)
        self.setWindowTitle("Main Window")
        self.setup_main_window()
        self.create_actions()
        self.create_menu()
        self.show()

    def setup_main_window(self):
        """Create and arrange widgets in the main window."""
        self.setCentralWidget(GraphicsView())

    def create_actions(self):
        """Create the application's menu actions."""
        # Create actions for File menu
        self.quit_act = QAction("&Quit")
        self.quit_act.setShortcut("Ctrl+Q")
        self.quit_act.triggered.connect(self.close)

    def create_menu(self):
        """Create the application's menu bar."""
        self.menuBar().setNativeMenuBar(False)
        # Create file menu and add actions
        file_menu = self.menuBar().addMenu("File")
        file_menu.addAction(self.quit_act)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MainWindow()
    sys.exit(app.exec_())

EDIT:

Example which use

  • left click to add rectangle (white background, black border)
  • first right click to select rectangle (red background)
  • second right click to put selected rectangle in new place (white background, black border)
import sys
from PySide6.QtWidgets import (
    QApplication,
    QMainWindow,
    QGraphicsView,
    QGraphicsScene,
    QGraphicsRectItem,
)
from PySide6.QtGui import QAction
from PySide6.QtCore import Qt

from PySide6.QtGui import QColor, QPen, QBrush


class GraphicsScene(QGraphicsScene):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setSceneRect(-100, -100, 200, 200)
        self.selected = []
        
    def mousePressEvent(self, event) -> None:
        if event.button() == Qt.LeftButton:
            #print("Left button pressed")
            x = event.scenePos().x()
            y = event.scenePos().y()
            #print(f"Position: {x}, {y}")

            rectitem = QGraphicsRectItem(0, 0, 10, 10)
            # set center of rectangle in mouse position
            rectitem.setPos(x-5, y-5)
            rectitem.setPen(QPen(QColor(0, 0, 0), 1.0, Qt.SolidLine))
            rectitem.setBrush(QBrush(QColor(255, 255, 255, 255)))

            self.addItem(rectitem)
            
        elif event.button() == Qt.RightButton:
            #print("Right button pressed")
            x = event.scenePos().x()
            y = event.scenePos().y()
            #print(f"Position: {x}, {y}")

            if self.selected:
                print('moved')
                for item in self.selected:
                    item.setPos(x-5, y-5)
                    item.setPen(QPen(QColor(0, 0, 0), 1.0, Qt.SolidLine))
                    item.setBrush(QBrush(QColor(255, 255, 255, 255)))
                self.selected.clear()
                
            else:
                for item in self.items():
                    pos = item.pos() 
                    if abs(x-pos.x()) < 10 and abs(y-pos.y()) < 10:
                        print('selected:', item)
                        self.selected.append(item)
                        item.setPen(QPen(QColor(255, 0, 0), 1.0, Qt.SolidLine))
                        item.setBrush(QBrush(QColor(255, 0, 0, 255)))
                        
class GraphicsView(QGraphicsView):
    def __init__(self):
        super().__init__()
        self.scene = self.setScene(GraphicsScene())


class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        """Set up the application's GUI."""
        self.setMinimumSize(450, 350)
        self.setWindowTitle("Main Window")
        self.setup_main_window()
        self.create_actions()
        self.create_menu()
        self.show()

    def setup_main_window(self):
        """Create and arrange widgets in the main window."""
        self.setCentralWidget(GraphicsView())

    def create_actions(self):
        """Create the application's menu actions."""
        # Create actions for File menu
        self.quit_act = QAction("&Quit")
        self.quit_act.setShortcut("Ctrl+Q")
        self.quit_act.triggered.connect(self.close)

    def create_menu(self):
        """Create the application's menu bar."""
        self.menuBar().setNativeMenuBar(False)
        # Create file menu and add actions
        file_menu = self.menuBar().addMenu("File")
        file_menu.addAction(self.quit_act)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MainWindow()
    sys.exit(app.exec_())

EDIT:

Version which uses mouseMoveEvent and mouseReleaseEvent to drag rect (keeping right click)

Based on code in answer to pyqt add rectangle in Qgraphicsscene

import sys
from PySide6.QtWidgets import (
    QApplication,
    QMainWindow,
    QGraphicsView,
    QGraphicsScene,
    QGraphicsRectItem,
    QGraphicsItem,
)
from PySide6.QtGui import QAction
from PySide6.QtCore import Qt

from PySide6.QtGui import QColor, QPen, QBrush, QTransform


class GraphicsScene(QGraphicsScene):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setSceneRect(-100, -100, 200, 200)
        self.selected = None
        self.selected_offset_x = 0
        self.selected_offset_y = 0
        
    def mousePressEvent(self, event) -> None:
        if event.button() == Qt.LeftButton:
            x = event.scenePos().x()
            y = event.scenePos().y()

            rectitem = QGraphicsRectItem(0, 0, 10, 10)
            rectitem.setPos(x-5, y-5)
            rectitem.setPen(QPen(QColor(0, 0, 0), 1.0, Qt.SolidLine))
            rectitem.setBrush(QBrush(QColor(255, 255, 255, 255)))
            #rectitem.setFlag(QGraphicsItem.ItemIsMovable, True)
            
            self.addItem(rectitem)
            
        elif event.button() == Qt.RightButton:
            x = event.scenePos().x()
            y = event.scenePos().y()

            if not self.selected:
                item = self.itemAt(event.scenePos(), QTransform())
                #print(item)
    
                if item:
                    print('selected:', item)
                    self.selected = item
                    self.selected.setBrush(QBrush(QColor(255, 0, 0, 255)))
                    self.selected_offset_x = x - item.pos().x()
                    self.selected_offset_y = y - item.pos().y()
                    #self.selected_offset_x = 5  # rect_width/2   # to keep center of rect
                    #self.selected_offset_y = 5  # rect_height/2  # to keep center of rect
        #super().mousePressEvent(event)
                    
    def mouseMoveEvent(self, event):
        #print('move:', event.button())
        #print('move:', event.buttons())
        if event.buttons() == Qt.RightButton:  # `buttons()` instead of `button()`
            if self.selected:
                print('moved')
                x = event.scenePos().x()
                y = event.scenePos().y()
                self.selected.setPos(x-self.selected_offset_x, y-self.selected_offset_y)
        #super().mouseMoveEvent(event)

    def mouseReleaseEvent(self, event):
        #print('release:', event.button())
        #print('release:', event.buttons())
        if event.button() == Qt.RightButton:
            if self.selected:
                print('released')
                self.selected.setBrush(QBrush(QColor(255, 255, 255, 255)))
                self.selected = None
        #super().mouseReleaseEvent(event)
        
class GraphicsView(QGraphicsView):
    def __init__(self):
        super().__init__()
        self.scene = self.setScene(GraphicsScene())


class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        """Set up the application's GUI."""
        self.setMinimumSize(450, 350)
        self.setWindowTitle("Main Window")
        self.setup_main_window()
        self.create_actions()
        self.create_menu()
        self.show()

    def setup_main_window(self):
        """Create and arrange widgets in the main window."""
        self.setCentralWidget(GraphicsView())

    def create_actions(self):
        """Create the application's menu actions."""
        # Create actions for File menu
        self.quit_act = QAction("&Quit")
        self.quit_act.setShortcut("Ctrl+Q")
        self.quit_act.triggered.connect(self.close)

    def create_menu(self):
        """Create the application's menu bar."""
        self.menuBar().setNativeMenuBar(False)
        # Create file menu and add actions
        file_menu = self.menuBar().addMenu("File")
        file_menu.addAction(self.quit_act)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MainWindow()
    sys.exit(app.exec_())

I was thinking about changing color when mouse hover item (using mouseMoveEvent) but at this moment I don't have it.

furas
  • 134,197
  • 12
  • 106
  • 148
  • Thank you so much for your help, i'll try to complete your code and post the full code whenever i can – YacineCino Jun 19 '22 at 13:42
  • I used code from some @eyllanesc answers - like [How to change the color of the rectangle when you click on the button "Color"](https://stackoverflow.com/questions/56076423/how-to-change-the-color-of-the-rectangle-when-you-click-on-the-button-color). And now I look at answer [Select items in QGraphicsScene using PySide?](https://stackoverflow.com/questions/46999042/select-items-in-qgraphicsscene-using-pyside). Probably it could use it to select item without checking distance. – furas Jun 19 '22 at 13:47
  • I added example which can drag rect (using right click) – furas Jun 19 '22 at 14:38
0

I found this code on here: https://python.tutorialink.com/drawing-straight-line-between-two-points-using-qpainterpath/ which lets you create a line with mousePress and move events

import sys
from PySide6.QtWidgets import (
    QApplication,
    QMainWindow,
    QGraphicsView,
    QGraphicsScene,
)
from PySide6.QtGui import QAction, QPainterPath
from PySide6.QtCore import Qt, QPointF


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

        self.path_item = self.addPath(QPainterPath())

        self.start_point = QPointF()
        self.end_point = QPointF()

    def mousePressEvent(self, event):
        self.start_point = event.scenePos()
        self.end_point = self.start_point
        self.update_path()
        super().mousePressEvent(event)

    def mouseMoveEvent(self, event):
        if event.buttons() & Qt.LeftButton:
            self.end_point = event.scenePos()
            self.update_path()
        super(GraphicsScene, self).mouseMoveEvent(event)

    def mouseReleaseEvent(self, event):
        self.end_point = event.scenePos()
        self.update_path()
        super(GraphicsScene, self).mouseReleaseEvent(event)

    def update_path(self):
        if not self.start_point.isNull() and not self.end_point.isNull():
            path = QPainterPath()
            path.moveTo(self.start_point)
            path.lineTo(self.end_point)
            self.path_item.setPath(path)


class GraphicsView(QGraphicsView):
    def __init__(self):
        super().__init__()
        self.scene = self.setScene(GraphicsScene())


class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        """Set up the application's GUI."""
        self.setMinimumSize(450, 350)
        self.setWindowTitle("Main Window")
        self.setup_main_window()
        self.create_actions()
        self.create_menu()
        self.show()

    def setup_main_window(self):
        """Create and arrange widgets in the main window."""
        self.setCentralWidget(GraphicsView())

    def create_actions(self):
        """Create the application's menu actions."""
        # Create actions for File menu
        self.quit_act = QAction("&Quit")
        self.quit_act.setShortcut("Ctrl+Q")
        self.quit_act.triggered.connect(self.close)

    def create_menu(self):
        """Create the application's menu bar."""
        self.menuBar().setNativeMenuBar(False)
        # Create file menu and add actions
        file_menu = self.menuBar().addMenu("File")
        file_menu.addAction(self.quit_act)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MainWindow()
    sys.exit(app.exec_())

EDIT :

I combined it with Furas's code and got this

import sys
from PySide6.QtWidgets import (
    QApplication,
    QMainWindow,
    QGraphicsView,
    QGraphicsScene,
    QGraphicsRectItem,
)
from PySide6.QtGui import QAction, QPainterPath
from PySide6.QtCore import Qt

from PySide6.QtGui import QColor, QPen, QBrush, QTransform


class GraphicsScene(QGraphicsScene):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setSceneRect(-100, -100, 200, 200)
        self.selected = None
        self.selected_offset_x = 0
        self.selected_offset_y = 0

    def mousePressEvent(self, event) -> None:
        if event.button() == Qt.LeftButton:
            x = event.scenePos().x()
            y = event.scenePos().y()

            # rectangle
            rectitem = QGraphicsRectItem(0, 0, 10, 10)
            rectitem.setPos(x - 5, y - 5)
            rectitem.setPen(QPen(QColor(0, 0, 0), 1.0, Qt.SolidLine))
            rectitem.setBrush(QBrush(QColor(255, 255, 255, 255)))
            # rectitem.setFlag(QGraphicsItem.ItemIsMovable, True)

            # Line
            self.path_item = self.addPath(QPainterPath())
            self.start_point = event.scenePos()
            self.end_point = self.start_point
            self.update_path()
            self.addItem(rectitem)

        elif event.button() == Qt.RightButton:
            x = event.scenePos().x()
            y = event.scenePos().y()

            if not self.selected:
                item = self.itemAt(event.scenePos(), QTransform())
                # print(item)

                if item:
                    print("selected:", item)
                    self.selected = item
                    self.selected.setBrush(QBrush(QColor(255, 0, 0, 255)))
                    self.selected_offset_x = x - item.pos().x()
                    self.selected_offset_y = y - item.pos().y()
                    # self.selected_offset_x = 5  # rect_width/2   # to keep center of rect
                    # self.selected_offset_y = 5  # rect_height/2  # to keep center of rect
        # super().mousePressEvent(event)

    def mouseMoveEvent(self, event):
        # print('move:', event.button())
        # print('move:', event.buttons())
        if event.buttons() == Qt.RightButton:  # `buttons()` instead of `button()`
            if self.selected:
                print("moved")
                x = event.scenePos().x()
                y = event.scenePos().y()
                self.selected.setPos(
                    x - self.selected_offset_x, y - self.selected_offset_y
                )
        elif event.buttons() == Qt.LeftButton:
            self.end_point = event.scenePos()
            self.update_path()
        # super().mouseMoveEvent(event)

    def mouseReleaseEvent(self, event):
        # print('release:', event.button())
        # print('release:', event.buttons())
        if event.button() == Qt.RightButton:
            if self.selected:
                print("released")
                self.selected.setBrush(QBrush(QColor(255, 255, 255, 255)))
                self.selected = None
        elif event.button() == Qt.LeftButton:
            self.end_point = event.scenePos()

            x = event.scenePos().x()
            y = event.scenePos().y()
            rectitem = QGraphicsRectItem(0, 0, 10, 10)
            rectitem.setPos(x - 5, y - 5)
            rectitem.setPen(QPen(QColor(0, 0, 0), 1.0, Qt.SolidLine))
            rectitem.setBrush(QBrush(QColor(255, 255, 255, 255)))
            self.addItem(rectitem)
            self.update_path()

        # super().mouseReleaseEvent(event)

    def update_path(self):
        if not self.start_point.isNull() and not self.end_point.isNull():
            path = QPainterPath()
            path.moveTo(self.start_point)
            path.lineTo(self.end_point)
            self.path_item.setPath(path)


class GraphicsView(QGraphicsView):
    def __init__(self):
        super().__init__()
        self.scene = self.setScene(GraphicsScene())


class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        """Set up the application's GUI."""
        self.setMinimumSize(450, 350)
        self.setWindowTitle("Main Window")
        self.setup_main_window()
        self.create_actions()
        self.create_menu()
        self.show()

    def setup_main_window(self):
        """Create and arrange widgets in the main window."""
        self.setCentralWidget(GraphicsView())

    def create_actions(self):
        """Create the application's menu actions."""
        # Create actions for File menu
        self.quit_act = QAction("&Quit")
        self.quit_act.setShortcut("Ctrl+Q")
        self.quit_act.triggered.connect(self.close)

    def create_menu(self):
        """Create the application's menu bar."""
        self.menuBar().setNativeMenuBar(False)
        # Create file menu and add actions
        file_menu = self.menuBar().addMenu("File")
        file_menu.addAction(self.quit_act)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MainWindow()
    sys.exit(app.exec_())

YacineCino
  • 33
  • 6