Reputation: 2842
I want to create a callback for a slider. But since the slider I made is part of a groupbox function. I am not sure how I can connect it:
def createExampleGroup(self, name, interval, steps, min, max):
groupBox = QGroupBox(name)
slider = QSlider(Qt.Horizontal)
slider.setFocusPolicy(Qt.StrongFocus)
slider.setTickPosition(QSlider.TicksBothSides)
slider.setMinimum(min)
slider.setMaximum(max)
slider.setTickInterval(interval)
slider.setSingleStep(steps)
vbox = QVBoxLayout()
vbox.addWidget(slider)
# vbox.addStretch(1)
groupBox.setLayout(vbox)
return groupBox
def valueChange(self):
print ("Update Slider")
self.aGroup = self.createExampleGroup("SliderA",10,10,10,100)
self.bGroup = self.createExampleGroup("SliderB",10,10,10,100)
So I am not sure how I can access the slider in each group and connect them to valueChange
. And also let valueChange()
do different update based on which slider it is. I tried self.aGroup.findChild(QSlider)
but it returns an address so I don't know how to use it. The reason I use group is that I may add other widgets in.
Upvotes: 1
Views: 740
Reputation: 244301
What findChild returns is the QSlider object, and you are probably printing it getting something similar to:
<PyQt5.QtWidgets.QSlider object at 0x7f6c521dedc8>
that's only what returns __str__
, you can find more information in How to print objects of class using print()?.
So I could use that object
slider = self.aGroup.findChild(QSlider)
slider.valueChanged.connect(self.valueChange)
Although that option can be a little dirty, a better option from the design point of view is to create a class that inherits from QGroupBox and that shares the signal:
class MyGroupBox(QGroupBox):
valueChanged = pyqtSignal(int)
def __init__(self, name, interval, steps, min, max):
super(MyGroupBox, self).__init__(name)
slider = QSlider(Qt.Horizontal)
slider.setFocusPolicy(Qt.StrongFocus)
slider.setTickPosition(QSlider.TicksBothSides)
slider.setMinimum(min)
slider.setMaximum(max)
slider.setTickInterval(interval)
slider.setSingleStep(steps)
slider.valueChanged.connect(self.valueChanged)
vbox = QVBoxLayout()
vbox.addWidget(slider)
# vbox.addStretch(1)
self.setLayout(vbox)
Then you can use it in the following way:
self.aGroup = MyGroupBox("SliderA",10,10,10,100)
self.bGroup = MyGroupBox("SliderB",10,10,10,100)
self.aGroup.valueChanged.connect(self.valueChange)
The above gives an identity since it is a class that manifests certain properties by abstracting the internal elements.
Upvotes: 1
Reputation: 6395
There is several options available to you. You can simply return several widgets from your function:
def create_example_group(...) # python naming convention!
...
return group_box, slider
self.a_group, a_slider = self.create_example_group(...)
a_slider.changeValue.connect(...)
Alternative you could give slider a unique name and use findChildren
or similar methods on your group-widget to find the widget.
Upvotes: 1