简体   繁体   中英

PyQt5, how to connect QSlider from a QGroupBox to a function

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.

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.

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.

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