[PyQt] Re: problem with connected scrollbars

Georg Altmann george at george-net.de
Wed Nov 12 11:13:43 GMT 2008


Vicent Mas schrieb:
> Hello,
> 
> I'm trying to make a QTableView with two 'connected' vertical scrollbars. By 
> connected I mean that the second scrollbar should be able to act on the first 
> one. As a simple case I want the first scrollbar to repeat the action 
> triggered by the second one. For instance, if I press the down arrow of the 
> second scrollbar I want the down arrow of the first scrollbar to be pressed 
> too. The attached script tries to do it. However an unexpected (unexpected 
> for me I mean :-) TypeError makes the script to fail. Could someone help me 
> with this problem, please?

You didn't write were the error actually occurs. Anyway I had the same 
problem of linking multiple scollbars. My solution is below. Of course 
this only makes sense if the sliders have the same range. Otherwise the 
class could be modified to use relative values. Depends on what you want 
to accomplish.

Use the class like this:

mySliderLinker = SliderLinker([scrollbar1, scrollbar2])

Regards
Georg


from PyQt4 import QtCore,QtGui
from PyQt4.QtCore import SIGNAL,SLOT


class SliderLinker(QtCore.QObject):
     '''Links multiple QAbstractSlider s together.

     If one slider's value changes, the other linked sliders will be set 

     to this value.'''

     def __init__(self, sliders, parent = None):
         super(SliderLinker,self).__init__(parent)
         self._sliders = sliders
         self._linkerSlots = []
         self.initSliders()

     def initSliders(self):
         for s in self._sliders:
             ls = LinkerSlot(self,s)
             self._linkerSlots.append(ls)

     def updateFrom(self,slider):
         val = slider.value()
         for s in self._sliders:
             if(s is not slider):
                 s.setValue(val)

class LinkerSlot(QtCore.QObject):
     def __init__(self,linker,slider, parent = None):
         super(LinkerSlot,self).__init__(parent)
         self._linker = linker
         self._slider = slider
         self.connect(slider, SIGNAL('valueChanged(int)'),
                 self.updateSliders)

     def updateSliders(self, value):
         self._linker.updateFrom(self._slider)




More information about the PyQt mailing list