简体   繁体   English

PyQt5,如何将QSlider从QGroupBox连接到函数

[英]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. 但是由于我制作的滑块是groupbox函数的一部分。 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 . 所以我不确定如何访问每个组中的滑块并将它们连接到valueChange And also let valueChange() do different update based on which slider it is. 并且还让valueChange()根据它是哪个滑块进行不同的更新。 I tried self.aGroup.findChild(QSlider) but it returns an address so I don't know how to use it. 我尝试了self.aGroup.findChild(QSlider)但是它返回一个地址,所以我不知道如何使用它。 The reason I use group is that I may add other widgets in. 我使用group的原因是我可以在其中添加其他小部件。

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. 或者,您可以给滑块指定一个唯一的名称,并在组小部件上使用findChildren或类似方法来找到小部件。

What findChild returns is the QSlider object, and you are probably printing it getting something similar to: findChild返回的是QSlider对象,您可能正在打印它,得到的内容类似于:

<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()? 这只是返回__str__ ,您可以在如何使用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: 尽管该选项可能有点脏,但从设计的角度来看,更好的选择是创建一个从QGroupBox继承并共享信号的类:

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. 上面给出了一个标识,因为它是一个通过抽象内部元素来显示某些属性的类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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