I’m working on a PyQt5 application for a course and need to implement a pop-up menu that appears when I right-click on the screen. I attempted to implement it, but I’m encountering an error stating that the PyQtSignal object has no connect attribute. According to the documentation, there should be a connect attribute, so I’m not sure what’s going wrong.
Here is the part of the code concerned:
import os,sys,json
from PyQt5 import QtCore,QtGui,QtWidgets
from PyQt5.QtWidgets import QMessageBox
#from scene import Scene
from view import View
class Window(QtWidgets.QMainWindow):
def __init__(self,position=(0,0),dimension=(500,300)):
QtWidgets.QMainWindow.__init__(self)
#...
self.view=View()
self.scene=QtWidgets.QGraphicsScene() # Model
self.view.setScene(self.scene)
self.setCentralWidget(self.view)
#...
self.create_actions()
self.connect_actions()
self.create_menus()
# ...
def connect_actions(self) :
# This doesnt work
#self.view.show_popup_menu.connect(self.show_popup_menu_slot)
# ...
def create_menus(self) :
#...
# Popup menu
self.popupMenu = QtWidgets.QMenu(self)
popup_menu_style_pen = self.popupMenu.addMenu(QtGui.QIcon("Icons/tool_pen.png"), '&Pen')
popup_menu_style_pen.addAction(self.action_style_pen_color)
popup_menu_style_pen.addAction(self.action_style_pen_width)
popup_menu_style_pen_line = popup_menu_style_pen.addMenu('&Line')
popup_menu_style_pen_line.addAction(self.action_line_solidline)
popup_menu_style_pen_line.addAction(self.action_line_dotline)
popup_menu_style_pen_line.addAction(self.action_line_dashdotline)
popup_menu_style_brush = self.popupMenu.addMenu(QtGui.QIcon("Icons/brush.png"), '&Brush')
popup_menu_style_brush.addAction(self.action_style_brush_color)
popup_menu_style_brush_fill = popup_menu_style_brush.addMenu(QtGui.QIcon("Icons/brush.png"), '&Fill')
popup_menu_style_brush_fill.addAction(self.action_style_brush_Cross)
popup_menu_style_brush_fill.addAction(self.action_style_brush_Solid)
popup_menu_style_brush_fill.addAction(self.action_style_brush_Horizontal)
popup_menu_style_brush_fill.addAction(self.action_style_brush_Vertical)
popup_menu_style_brush_fill.addAction(self.action_style_brush_DiagCross)
popup_menu_style_brush_fill.addAction(self.action_style_brush_None)
def show_popup_menu_slot(self):
self.popupMenu.exec_(QtGui.QCursor.pos())
The show_popupmenu is defined in the view constructor in an other file like this:
self.show_popup_menu = QtCore.pyqtSignal()
We have to build our project in those 2 files
I tried to look on the internet, i found stack overflow examples with PyQt4 but it doesnt work in my case