简体   繁体   中英

pyqt4: connect a slider to refresh a plot

I am trying to design a GUI to refresh a plot ie plot(timeS, dataS) , using a slider corresponding to the time vector ( timeS ) of the data that I want to display ( dataS ) which is varying in time.

I have tried to connect the slider to my plot function which has a lot of input self.slider.valueChanged.connect(self.myPlotFunction(.....)) , which is located in the init part of my class, but I can't use the input of the defined function myPlotFunction() .

So I want that when I start dragging the slider, the plot changes according to the time/position of the slider. Moreover my time vector is made of floats while the slider allows int values.

When you connect a slot, you don't call the function, so

self.slider.valueChanged.connect(self.myPlotFunction(.....))

should look like

self.slider.valueChanged.connect(self.myPlotFunction)

and your definition of myPlotFunction must match the argument signature of the valueChanged signal, so

def myPlotFunction(self, sliderValue):
    # Do stuff
    pass

If you need to pass more stuff to myPlotFunction , you can use a lambda, making sure to accomodate the arguments that Qt will pass first, ie

otherVar = "some other stuff"
self.slider.valueChanged.connect(lambda sv, ov=otherVar: self.myPlotFunction(sv, ov))

and adjust the definition of myPlotFunction accordingly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM