Correct way to convert old SIGNAL and SLOT to new style?

为君一笑 提交于 2019-12-06 03:29:29

The exact answer to this will depend on what kind of object self is. If it is a Qt class that already defines those signals, then the new-style syntax would be this:

self.currentChanged[int, int].emit(row, col)
self.activated[str].emit(self.currentText())
self.currentChanged[str].connect(self.handleCurrentChanged)

However, if any of those aren't pre-defined, you would need to define custom signals for them, like this:

class MyClass(QWidget):
    # this defines two overloads for currentChanged
    currentChanged = QtCore.pyqtSignal([int, int], [str])
    activated = QtCore.pyqtSignal(str)

    def __init__(self, parent=None):
        super(MyClass, self).__init__(parent)
        self.currentChanged[str].connect(self.handleCurrentChanged)   

    def handleCurrentChanged(self, text):
        print(text)

The old-style syntax allowed custom signals to be emitted dynamically (i.e. without defining them first), but that is not possible any more. With the new-style syntax, custom signals must always be explicitly defined.

Note that, if there is only one overload defined for a signal, the selector can be omitted:

    self.activated.emit(self.currentText())

For more information, see these articles in the PyQt Docs:

EDIT:

For your actual code, you need to make the following changes for the currentChanged signals:

  1. In Multibar.py (around line 30):

    This defines a custom signal (because QWidget does not have it):

    class MultiTabBar(QWidget):
        # add the following line
        currentChanged = pyqtSignal(int, int)
    
  2. In Multibar.py (around line 133):

    This emits the custom signal defined in (1):

    # self.emit(SIGNAL('currentChanged'), row, col)
    self.currentChanged.emit(row, col)
    
  3. In ScWindow.py (around line 478):

    This connects the signal defined in (1):

        # self.connect(self.PieceTab,SIGNAL("currentChanged"),self.pieceTabChanged)
        self.PieceTab.currentChanged.connect(self.pieceTabChanged)
    
  4. In ItemList.py (around line 73):

    The QFileDialog class already defines this signal, and there is only one overload of it. But the name of the slot must be changed, because it is shadowing the built-in signal name (which has become an attribute in the new-style syntax). So the connection should be made like this:

        # self.connect(self,SIGNAL("currentChanged(const QString&)"),self.currentChanged)
        self.currentChanged.connect(self.onCurrentChanged)
    
  5. In ItemList.py (around line 78):

    This renames the slot for the connection made in (4):

        # def currentChanged(self, file):
        def onCurrentChanged(self, file):
    
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!