Use QT slider value in a python calculation?

  • Hi,

    I just started using Qt designer on friday, looked at some youtube tutorials and i really like it.

    I have a pile of python code - i used to run it and either change variables in the code or using user input but I'd like to add a really simple UI. To practice a bit - I created a simple widget with two sliders and linked them to two text boxes. I then created the Python code using my command prompt. I've spent the afternoon googling but can't seem to find an example that shows me how to take the slider values (or dials, or whatever) and use them in a simple calculation.

    What is the name of the Slider variable?

    Thanks in advance.

    @from PyQt4 import QtCore, QtGui

    _fromUtf8 = QtCore.QString.fromUtf8
    except AttributeError:
    _fromUtf8 = lambda s: s

    class Ui_Form(object):
    def setupUi(self, Form):
    Form.resize(812, 576)
    Form.setWindowTitle(QtGui.QApplication.translate("Form", "Form", None, QtGui.QApplication.UnicodeUTF8))
    self.horizontalSlider = QtGui.QSlider(Form)
    self.horizontalSlider.setGeometry(QtCore.QRect(60, 220, 160, 21))
    self.verticalSlider = QtGui.QSlider(Form)
    self.verticalSlider.setGeometry(QtCore.QRect(300, 160, 21, 160))
    self.label = QtGui.QLabel(Form)
    self.label.setGeometry(QtCore.QRect(80, 170, 111, 41))
    self.label.setText(QtGui.QApplication.translate("Form", "TextLabel", None, QtGui.QApplication.UnicodeUTF8))
    self.label_2 = QtGui.QLabel(Form)
    self.label_2.setGeometry(QtCore.QRect(290, 110, 53, 16))
    self.label_2.setText(QtGui.QApplication.translate("Form", "TextLabel", None, QtGui.QApplication.UnicodeUTF8))

        QtCore.QObject.connect(self.horizontalSlider, QtCore.SIGNAL(_fromUtf8("valueChanged(int)")), self.label.setNum)
        QtCore.QObject.connect(self.verticalSlider, QtCore.SIGNAL(_fromUtf8("valueChanged(int)")), self.label_2.setNum)
    def retranslateUi(self, Form):

    if name == "main":
    import sys
    app = QtGui.QApplication(sys.argv)
    Form = QtGui.QWidget()
    ui = Ui_Form()

  • I've been working on this and changed it slightly - i also added a seperate main function. i added ok and cancel buttons to see if they could give me a clue - I think im missing a exec_() but have no idea where to put it. I'm also not sure where to put the print ui.horizontalSlider.value() and print ui.verticalSlider.value() - in main? I need to access these values in multiple calculations, in multiple functions.

    @from PyQt4 import QtCore, QtGui

    _fromUtf8 = QtCore.QString.fromUtf8
    except AttributeError:
    _fromUtf8 = lambda s: s

    class Ui_Dialog(object):

    def setupUi(self, Dialog):
        Dialog.resize(682, 539)
        Dialog.setWindowTitle(QtGui.QApplication.translate("Dialog", "Dialog", None, QtGui.QApplication.UnicodeUTF8))
        self.buttonBox = QtGui.QDialogButtonBox(Dialog)
        self.buttonBox.setGeometry(QtCore.QRect(10, 440, 621, 32))
        self.horizontalSlider = QtGui.QSlider(Dialog)
        self.horizontalSlider.setGeometry(QtCore.QRect(60, 190, 160, 21))
        self.verticalSlider = QtGui.QSlider(Dialog)
        self.verticalSlider.setGeometry(QtCore.QRect(360, 140, 21, 160))
        self.label = QtGui.QLabel(Dialog)
        self.label.setGeometry(QtCore.QRect(110, 140, 53, 16))
        self.label.setText(QtGui.QApplication.translate("Dialog", "TextLabel", None, QtGui.QApplication.UnicodeUTF8))
        self.label_2 = QtGui.QLabel(Dialog)
        self.label_2.setGeometry(QtCore.QRect(340, 90, 53, 16))
        self.label_2.setText(QtGui.QApplication.translate("Dialog", "TextLabel", None, QtGui.QApplication.UnicodeUTF8))
        QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL(_fromUtf8("accepted()")), Dialog.accept)
        QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL(_fromUtf8("rejected()")), Dialog.reject)
        QtCore.QObject.connect(self.horizontalSlider, QtCore.SIGNAL(_fromUtf8("valueChanged(int)")), self.label.setNum)
        QtCore.QObject.connect(self.verticalSlider, QtCore.SIGNAL(_fromUtf8("valueChanged(int)")), self.label_2.setNum)
    def retranslateUi(self, Dialog):

    And the main function


    from PyQt4 import QtCore, QtGui
    from PyQt4.QtCore import *
    from PyQt4.QtGui import *
    from MIMIC_ver1 import Ui_Dialog
    import sys, os

    class Dialog(QDialog, Ui_Dialog):
    def init(self):

    def main():
    app = QtGui.QApplication(sys.argv)
    Dialog = QtGui.QDialog()
    ui = Ui_Dialog()
    print ui.horizontalSlider.value()
    print ui.verticalSlider.value()

    if name == 'main':

  • Lifetime Qt Champion


    Are your calculation functions in a library ? If so I would suggest the following:

    • Create a QObject to manage your various calculation logic
    • Give that QObject slots to get the values from the sliders
    • Use signal/slots to send the values from your UI to that object
    • Add slots to the object to start the calculations
    • Be aware that if the calculations are heavy that simple approach might freeze your UI

    In a second round you might be interested by QtConcurrent for the math part.

    Hope it helps

  • Thanks for the reply - my calculations are in python classes and functions that i created myself. They are variables that are entered in the user-input stage like this

    a = (float(raw_input('Enter angle of rotation'))

    I would like to make it so the slider in Qt becomes the method of user input. I've been searching for ages and amazed how rarely it comes up - I must be missing something simple or misunderstanding.

  • Hi All,

    I'm still struggling with this one. here's a different example I've been using. I added the print function - so now it is printing the slider value for me. I have a few questions though -

    1. I don't see how it's getting the slider value - in the example they pass 'value' but value isn't defined anywhere. Is it a hardcoded function in QT?

    2. Say for example i wanted to use the slider value in loads of calculations - do i just use value?

    3. how do I make it the final slider value? Add an ok option?

    Thanks in advance - Conor.


    -- coding: utf-8 --

    ZetCode PyQt4 tutorial

    This example shows a QtGui.QSlider widget.

    author: Jan Bodnar
    last edited: September 2011

    import sys
    from PyQt4 import QtGui, QtCore

    class Example(QtGui.QWidget):

    def __init__(self):
        super(Example, self).__init__()
    def initUI(self):      
        sld = QtGui.QSlider(QtCore.Qt.Horizontal, self)
        sld.setGeometry(30, 40, 100, 30)
        self.label = QtGui.QLabel(self)
        self.label.setGeometry(160, 40, 80, 30)
        self.setGeometry(300, 300, 280, 170)
    def changeValue(self, value):
        print value

    def main():

    app = QtGui.QApplication(sys.argv)
    ex = Example()

    if name == 'main':

  • Lifetime Qt Champion

    It's because QSlider has a signal called (C++ style)

    @void valueChanged(int value);@

    So your slot must have the same or less argument. Since you are using python, you don't have to worry the same way about types than you do with C++

    For your slots, you'd better use something meaning full like:
    @ sld.valueChanged[int].connect(self.setRotationAngle)

    def setRotationAngle(self, angle)
    ### Do the updates then start your calculations

    That will work the same and your code will be easier to read.

    I'm not sure understand question number 3 correctly. If you are thinking about the slider range i.e. 0-359° then you can simply set the slider range
    @self.sld.setRange(0, 359)@

  • I get you - thanks for that.

    In Question No. 3 I meant that is there a way to use only the final value of the slider. At present it prints every value it slides through on the way to the user defined setting.

    I suppose I need an ok button to signify 'final value'?

  • Lifetime Qt Champion

    If you only wish to do your calculation once the user has set all input variables, then indeed, have a button that says something like: tr("Start").

    You can then do things a bit differently: you have a "setup" widget containing your sliders and other controls, at the bottom you have the start button that is connected to a slot. In this slot, you query the values of the different sliders and other useful input widgets and do your calculation. Don't forget to give the user some feedback i.e. QProgressDialog to let him know that something is being done.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.