Python信号和类之间的插槽

时间:2014-08-18 02:30:51

标签: python qt pyqt

我正在尝试在Python / PyQt中获取一个Gui应用程序,让一个sliderDialog类通过信号和发射向MainWindow发送一个缩放器列表....但是下面的代码不起作用。滑块改变了值......但是MainWindow没有看到信号,我认为这些信号会通知MainWindow。但不是。救命?哦 - 我知道我还没有设置通过定标器值,但是因为信号不能解决这个问题。

from PyQt4 import QtCore
from PyQt4 import QtGui

OFFSET_Y = 15
OFFSET_X = 5
SPACER_Y = 20
SPACER_X = 50
switch = [1.0, 2.0, 3.0, 6.0, 9.0, 12.0, 18.0]

class MainWindow(QtGui.QMainWindow):

    sliderUpdate = QtCore.SIGNAL('sliderUpdate()')

    def __init__(self):
        super(MainWindow, self).__init__()
        sdlg = SliderDialog(self, 5)
        sdlg.connect(self, QtCore.SIGNAL('sliderUpdate'), self.scalers)
        sdlg.show()

    def scalers(self) :
        print "In scalers of MainWindow!"

class SliderDialog(QtGui.QDialog) :

    sliderMoved  = QtCore.pyqtSignal()

    def __init__( self, parent, dataCount ) :
        super(SliderDialog, self).__init__(parent)

        sliderLayout = QtGui.QVBoxLayout()
        sliderLayout.addStretch()

        self.slider = []
        self.sliderLabel = []
        for i in range(dataCount) :

            s = QtGui.QSlider(QtCore.Qt.Horizontal, self)
            s.setMinimum(0)
            s.setMaximum(len(switch)-1)
            s.setTickInterval(1)
            s.setSingleStep(1)
            s.move(170, OFFSET_Y+30 + 3*SPACER_Y*i)
            s.sliderReleased.connect(self.sliderMoved)

            sl = QtGui.QLabel("Scaling to : 1.0", self)
            sl.move(5, OFFSET_Y+30 + 3*SPACER_Y*i)

            self.slider.append(s)
            self.sliderLabel.append(sl)

            sliderLayout.addWidget(self.slider[i])
            sliderLayout.addWidget(self.sliderLabel[i])

    def sliderMoved(self) :
        for i, slides in enumerate(self.slider) :
            scaler = slides.sliderPosition()
            self.sliderLabel[i].setText("Scaling to: {}".format(switch[scaler]))
            self.emit(QtCore.SIGNAL('sliderUpdate'))

if __name__ == '__main__':

    import sys

    app = QtGui.QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())

1 个答案:

答案 0 :(得分:1)

问题是'connect'的put参数错误;

bool QObject.connect (QObject, SIGNAL(), callable, Qt.ConnectionType = Qt.AutoConnection)

参数:

1. QObject  : Source object to receive emit signal

2. SIGNAL() : Signal name

3. callable : Callable do your want go it then have signal from source object

4. (Optional)

要修复它们,请在正确位置更改右侧参数。

这;

sdlg.connect(self, QtCore.SIGNAL('sliderUpdate'), self.scalers)

要;

self.connect(sdlg, QtCore.SIGNAL('sliderUpdate'), self.scalers)

参考http://pyqt.sourceforge.net/Docs/PyQt4/qobject.html#connect-2


上次编辑2014年8月18日11:37 :添加完整代码;

from PyQt4 import QtCore
from PyQt4 import QtGui

OFFSET_Y = 15
OFFSET_X = 5
SPACER_Y = 20
SPACER_X = 50
switch = [1.0, 2.0, 3.0, 6.0, 9.0, 12.0, 18.0]

class MainWindow(QtGui.QMainWindow):

    sliderUpdate = QtCore.SIGNAL('sliderUpdate()')

    def __init__(self):
        super(MainWindow, self).__init__()
        sdlg = SliderDialog(self, 5)
        self.connect(sdlg, QtCore.SIGNAL('sliderUpdate'), self.scalers)
        sdlg.show()

    def scalers(self) :
        print "In scalers of MainWindow!"

class SliderDialog(QtGui.QDialog) :

    sliderMoved  = QtCore.pyqtSignal()

    def __init__( self, parent, dataCount ) :
        super(SliderDialog, self).__init__(parent)

        sliderLayout = QtGui.QVBoxLayout()
        sliderLayout.addStretch()

        self.slider = []
        self.sliderLabel = []
        for i in range(dataCount) :

            s = QtGui.QSlider(QtCore.Qt.Horizontal, self)
            s.setMinimum(0)
            s.setMaximum(len(switch)-1)
            s.setTickInterval(1)
            s.setSingleStep(1)
            s.move(170, OFFSET_Y+30 + 3*SPACER_Y*i)
            s.sliderReleased.connect(self.sliderMoved)

            sl = QtGui.QLabel("Scaling to : 1.0", self)
            sl.move(5, OFFSET_Y+30 + 3*SPACER_Y*i)

            self.slider.append(s)
            self.sliderLabel.append(sl)

            sliderLayout.addWidget(self.slider[i])
            sliderLayout.addWidget(self.sliderLabel[i])

    def sliderMoved(self) :
        for i, slides in enumerate(self.slider) :
            scaler = slides.sliderPosition()
            self.sliderLabel[i].setText("Scaling to: {}".format(switch[scaler]))
            self.emit(QtCore.SIGNAL('sliderUpdate'))

if __name__ == '__main__':

    import sys

    app = QtGui.QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())

此致