Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Use QT slider value in a python calculation?
Forum Updated to NodeBB v4.3 + New Features

Use QT slider value in a python calculation?

Scheduled Pinned Locked Moved General and Desktop
8 Posts 2 Posters 10.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    con20or
    wrote on last edited by
    #1

    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

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

    class Ui_Form(object):
    def setupUi(self, Form):
    Form.setObjectName(_fromUtf8("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.horizontalSlider.setMinimum(-179)
    self.horizontalSlider.setMaximum(179)
    self.horizontalSlider.setOrientation(QtCore.Qt.Horizontal)
    self.horizontalSlider.setObjectName(_fromUtf8("horizontalSlider"))
    self.verticalSlider = QtGui.QSlider(Form)
    self.verticalSlider.setGeometry(QtCore.QRect(300, 160, 21, 160))
    self.verticalSlider.setMinimum(0)
    self.verticalSlider.setMaximum(90)
    self.verticalSlider.setOrientation(QtCore.Qt.Vertical)
    self.verticalSlider.setObjectName(_fromUtf8("verticalSlider"))
    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.setObjectName(_fromUtf8("label"))
    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))
    self.label_2.setObjectName(_fromUtf8("label_2"))

        self.retranslateUi(Form)
        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)
        QtCore.QMetaObject.connectSlotsByName(Form)
    
    def retranslateUi(self, Form):
        pass
    

    if name == "main":
    import sys
    app = QtGui.QApplication(sys.argv)
    Form = QtGui.QWidget()
    ui = Ui_Form()
    ui.setupUi(Form)
    Form.show()
    sys.exit(app.exec_())@

    1 Reply Last reply
    0
    • C Offline
      C Offline
      con20or
      wrote on last edited by
      #2

      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

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

      class Ui_Dialog(object):

      def setupUi(self, Dialog):
          Dialog.setObjectName(_fromUtf8("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.buttonBox.setOrientation(QtCore.Qt.Horizontal)
          self.buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Cancel|QtGui.QDialogButtonBox.Ok)
          self.buttonBox.setObjectName(_fromUtf8("buttonBox"))
          self.horizontalSlider = QtGui.QSlider(Dialog)
          self.horizontalSlider.setGeometry(QtCore.QRect(60, 190, 160, 21))
          self.horizontalSlider.setOrientation(QtCore.Qt.Horizontal)
          self.horizontalSlider.setObjectName(_fromUtf8("horizontalSlider"))
          self.verticalSlider = QtGui.QSlider(Dialog)
          self.verticalSlider.setGeometry(QtCore.QRect(360, 140, 21, 160))
          self.verticalSlider.setOrientation(QtCore.Qt.Vertical)
          self.verticalSlider.setObjectName(_fromUtf8("verticalSlider"))
          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.setObjectName(_fromUtf8("label"))
          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))
          self.label_2.setObjectName(_fromUtf8("label_2"))
      
          self.retranslateUi(Dialog)
          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)
          QtCore.QMetaObject.connectSlotsByName(Dialog)
      
      def retranslateUi(self, Dialog):
          pass@
      

      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):
      QDialog.init(self)
      self.setupUi(self)

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

      if name == 'main':
      main()
      @

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        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

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • C Offline
          C Offline
          con20or
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • C Offline
            C Offline
            con20or
            wrote on last edited by
            #5

            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.

            @
            #!/usr/bin/python

            -- coding: utf-8 --

            """
            ZetCode PyQt4 tutorial

            This example shows a QtGui.QSlider widget.

            author: Jan Bodnar
            website: zetcode.com
            last edited: September 2011
            """

            import sys
            from PyQt4 import QtGui, QtCore

            class Example(QtGui.QWidget):

            def __init__(self):
                super(Example, self).__init__()
                
                self.initUI()
                
            def initUI(self):      
            
                sld = QtGui.QSlider(QtCore.Qt.Horizontal, self)
                sld.setFocusPolicy(QtCore.Qt.NoFocus)
                sld.setGeometry(30, 40, 100, 30)
                sld.valueChanged[int].connect(self.changeValue)
                
                self.label = QtGui.QLabel(self)
                self.label.setPixmap(QtGui.QPixmap('mute.png'))
                self.label.setGeometry(160, 40, 80, 30)
                
                self.setGeometry(300, 300, 280, 170)
                self.setWindowTitle('QtGui.QSlider')
                self.show()
                
            def changeValue(self, value):
                print value
            

            def main():

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

            if name == 'main':
            main()
            @

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              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)@

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • C Offline
                C Offline
                con20or
                wrote on last edited by
                #7

                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'?

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  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.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved