IanRoberts IanRoberts - 1 month ago 19
Python Question

Right click functionality in PyQT widget broken from PyQT4 to PyQT5 update

I previously developed an application with PyQT4, including a QListWidget, with a right-click-pop-up-window that allowed the user to quickly delete rows from the widget.

This was based on code from Stack Overflow: PyQt: How to get most of QListWidget

Unfortunately, upon upgrading to PYQt5, this functionality is now broken. I get the error: QListWidget object has no attribute 'connect'.

I suspect this is a due to a new implementation in PyQt5, which is quite annoying, although I can't find a simple way to fix this from the new Qt documentation.

Could someone suggest how I might restore my original functionality please?

I'd prefer to not roll back to Qt5 in the interest of future-proofing.

Answer

In PyQt5, PyQt4's old-style signals and slots are not supported.

So, to fix the problem, replace the line

        self.myListWidget.connect(self.myListWidget, QtCore.SIGNAL("customContextMenuRequested(QPoint)" ), self.listItemRightClicked)

with

        self.myListWidget.customContextMenuRequested.connect(self.listItemRightClicked)

and replace the line

        self.connect(menu_item, QtCore.SIGNAL("triggered()"), self.menuItemClicked) 

with

        menu_item.triggered.connect(self.menuItemClicked)
Comments