PyQt QSpinBox update range depending on the value

2020-03-30 09:20发布

问题:

i am using pyqt4 to develop a GUI for the first time;

I have a spinbox, and I would like the range of values allowed in it to be dependant on the value another spinbox. for example, the maximum value allowed in the first spinbox should be equal to the value of the second spinbox.

I thought this may be possible using a valueChanged() signal to call a method that is something like:

def function
    max = spinbox2.value()
    spinbox1.setMaximum(max)

but that didnt work, does anyone know how to do this?

thank you

回答1:

You have not shown your code where you make the connection between the 'valueChanged' signal of spinbox2 with the function. Are you making that connection ?. Also the code you have given for function seems to be incomplete.

You can try something like this:

spinbbox2.valueChanged.connect(handler)
# Or this which works for Python 3.5
spinbbox2.valueChanged[int].connect(handler)

# The function handler called is called whenever
# value in spinbox2 is changed and parameter to
# handler is the new value of spinbox2
def handler(value):
    spinbox1.setMaximum(value)