Search code examples
pythonpython-3.xpyqtpyqt4pyqt5

To PyQt5 'connect' conversion


I try to make a conversion of some code from PyQt4 to PyQt5, however I have not ever worked with PyQt, that is why I have some problems with this. I failed to convert some code with .connect, because as I understand in PyQt5 work with signals and slots changed. Here is the code.

class Gui(QWidget.QMainWindow, Ui_MainWindow):

    def __init__(self, cfgpath):
        QWidget.QMainWindow.__init__(self)
        Ui_MainWindow.__init__(self)

        self.configpath = cfgpath
        self.paint = Viewer(self)
        self.setupUi(self)

        self.loadButton.clicked.connect(self.loadImage)
        self.maskClearButton.clicked.connect(self.paint.clearMask)
        self.brushSizeSB.valueChanged.connect(self.brushSizeChange)
        btnlist = [self.horDownBtn, self.horDownLargeBtn, self.horUpBtn, self.horUpLargeBtn,
                   self.vertDownBtn, self.vertDownLargeBtn, self.vertUpBtn, self.vertUpLargeBtn]
        sigmap = QtCore.QSignalMapper(self)
        for i in range(len(btnlist)):
            # Here it falls
            self.connect(btnlist[i], QtCore.SIGNAL("clicked()"), sigmap, QtCore.SLOT("map()"))
            btnlist[i].clicked
            sigmap.setMapping(btnlist[i], i)
        # And here
        self.connect(sigmap, QtCore.SIGNAL("mapped(int)"), self.paint.handleScaleBtn)

So how can I change this code to make it work?


Solution

  • The new connection style is as follows:

    sender.signal.connect(slot)
    

    In your case change:

    self.connect(btnlist[i], QtCore.SIGNAL("clicked()"), sigmap, QtCore.SLOT("map()"))
    
    self.connect(sigmap, QtCore.SIGNAL("mapped(int)"), self.paint.handleScaleBtn)
    

    to

    btnlist[i].clicked.connect(sigmap.map)
    
    sigmap.mapped.connect(self.paint.handleScaleBtn)