2

In my QGIS Plugin I have this Qt dialog where there are two horizontal sliders. I'm trying to hook one to the other in this way: if the checkBox is checked, I take the difference between them and if one is moved the other change the value using the difference. This is the code I tried, but it does not work.

def run (self):
   self.dlg = Dialog()
   if self.dlg.checkBox.isChecked():
      firstDate = self.dlg.horizontalSlider.value()
      secondDate = self.dlg.horizontalSlider_2.value()
      difference = abs(secondDate-firstDate)
      self.dlg.horizontalSlider.sliderMoved.connect(self.dlg.horizontalSlider_2.setValue(firstDate+difference))
      self.dlg.horizontalSlider_2.sliderMoved.connect(self.dlg.horizontalSlider.setValue(secondDate-difference))
   self.dlg.show()

This is the dialog to be more clear:

enter image description here

2 Answers 2

4

Test-it in another method like that :

def run (self):
   self.dlg = Dialog()
   self.dlg.horizontalSlider.sliderMoved.connect(lambda v: self.adjust_sliders(1, v))
   self.dlg.horizontalSlider_2.sliderMoved.connect(lambda v: self.adjust_sliders(2, v))
   self.dlg.show()

def adjust_sliders(self, slider_num, moved_value):
   if self.dlg.checkBox.isChecked():
      firstDate = self.dlg.horizontalSlider.value()
      secondDate = self.dlg.horizontalSlider_2.value()
      difference = abs(secondDate-firstDate)

   if slider_num == 1:
      self.dlg.horizontalSlider_2.sliderMoved.disconnect()
      self.dlg.horizontalSlider_2.setValue(firstDate+difference)
      self.dlg.horizontalSlider_2.sliderMoved.connect(lambda v: self.adjust_sliders(1, v))
   if slider_num == 2:
      self.dlg.horizontalSlider.sliderMoved.disconnect()
      self.dlg.horizontalSlider.setValue(secondDate-difference)
      self.dlg.horizontalSlider.sliderMoved.connect(lambda v: self.adjust_sliders(2, v))

But beware, if you move slider 1, slider 2 will move that will move slider 1, etc. This will cause an infinite recursion.

It's normally now corrected.

5
  • Thanks, do you have any idea to avoid the recursion problem?
    – Lorenzo
    Feb 20, 2020 at 20:03
  • @Lorenzo: yes, disconnect the signal before setting the new value, reconnect it just after. Your solution is false, connect the signals only in the run part. Feb 20, 2020 at 20:09
  • I'm sorry, but I don't know what this means. Could you show me how to do that? Thanks again
    – Lorenzo
    Feb 20, 2020 at 20:14
  • See my post edit. Feb 20, 2020 at 20:14
  • Many thanks, now it works.
    – Lorenzo
    Feb 20, 2020 at 20:25
0

Thanks to J. Monticolo suggestion, I solved in this way.

def run(self):  
  self.dlg = Dialog() 
  self.dlg.checkBox.stateChanged.connect(lambda: self.takeDifference())
  self.dlg.show()     

def takeDifference(self):
  if self.dlg.checkBox.isChecked():
    firstDate = self.dlg4.horizontalSlider.value()
    secondDate = self.dlg4.horizontalSlider_2.value()
    difference = abs(secondDate-firstDate)
    self.dlg.horizontalSlider.sliderMoved.connect(lambda: self.adjust_sliders(1,difference))
    self.dlg.horizontalSlider_2.sliderMoved.connect(lambda: self.adjust_sliders(2,difference))

def adjust_sliders(self, slider_num,difference):
  if self.dlg.checkBox.isChecked():
    firstDate = self.dlg.horizontalSlider.value()
    secondDate = self.dlg.horizontalSlider_2.value()
    if slider_num == 1:
      self.dlg.horizontalSlider_2.valueChanged.disconnect()
      self.dlg.horizontalSlider_2.setValue(firstDate+difference)
      self.dlg.horizontalSlider_2.valueChanged.connect(lambda: self.adjust_sliders(1,difference))
    if slider_num == 2:
       self.dlg.horizontalSlider.valueChanged.disconnect()
       self.dlg.horizontalSlider.setValue(secondDate-difference)
       self.dlg.horizontalSlider.valueChanged.connect(lambda: self.adjust_sliders(2,difference))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.