qt - Python signals and slots between classes -
i trying gui app in python/pyqt have sliderdialog class send list of scalers mainwindow via signals , emits .... following code not work. sliders change value ... mainwindow not see signals, thought emit inform mainwindow. no. help? oh - know have not set pass scaler values, since signals don't work not pursuing this.
from pyqt4 import qtcore 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 = [] 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 : 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) : 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_())
problem put parameter of 'connect
' wrong;
bool qobject.connect (qobject, signal(), callable, qt.connectiontype = qt.autoconnection)
parameter:
1. qobject : source object receive emit signal 2. signal() : signal name 3. callable : callable want go have signal source object 4. (optional)
to fix them, please change right parameter in right position.
from;
sdlg.connect(self, qtcore.signal('sliderupdate'), self.scalers)
to;
self.connect(sdlg, qtcore.signal('sliderupdate'), self.scalers)
reference : http://pyqt.sourceforge.net/docs/pyqt4/qobject.html#connect-2
last edited 18 / 8/ 2014 11 : 37 : add full code;
from pyqt4 import qtcore 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 = [] 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 : 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) : 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_())
regards,
Comments
Post a Comment