1. <legend id='v5APp'><style id='v5APp'><dir id='v5APp'><q id='v5APp'></q></dir></style></legend>

    1. <i id='v5APp'><tr id='v5APp'><dt id='v5APp'><q id='v5APp'><span id='v5APp'><b id='v5APp'><form id='v5APp'><ins id='v5APp'></ins><ul id='v5APp'></ul><sub id='v5APp'></sub></form><legend id='v5APp'></legend><bdo id='v5APp'><pre id='v5APp'><center id='v5APp'></center></pre></bdo></b><th id='v5APp'></th></span></q></dt></tr></i><div id='v5APp'><tfoot id='v5APp'></tfoot><dl id='v5APp'><fieldset id='v5APp'></fieldset></dl></div>
      • <bdo id='v5APp'></bdo><ul id='v5APp'></ul>
      <tfoot id='v5APp'></tfoot>

      <small id='v5APp'></small><noframes id='v5APp'>

      对 QSlider 使用浮点数

      时间:2023-08-05
          <tbody id='7J6UG'></tbody>
          <bdo id='7J6UG'></bdo><ul id='7J6UG'></ul>
          1. <tfoot id='7J6UG'></tfoot>

              • <legend id='7J6UG'><style id='7J6UG'><dir id='7J6UG'><q id='7J6UG'></q></dir></style></legend>

                <small id='7J6UG'></small><noframes id='7J6UG'>

                <i id='7J6UG'><tr id='7J6UG'><dt id='7J6UG'><q id='7J6UG'><span id='7J6UG'><b id='7J6UG'><form id='7J6UG'><ins id='7J6UG'></ins><ul id='7J6UG'></ul><sub id='7J6UG'></sub></form><legend id='7J6UG'></legend><bdo id='7J6UG'><pre id='7J6UG'><center id='7J6UG'></center></pre></bdo></b><th id='7J6UG'></th></span></q></dt></tr></i><div id='7J6UG'><tfoot id='7J6UG'></tfoot><dl id='7J6UG'><fieldset id='7J6UG'></fieldset></dl></div>
              • 本文介绍了对 QSlider 使用浮点数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我有一个 QLineEdit 和一个 QSlider,它们在其中相互交互.

                I have a QLineEdit and a QSlider in which it interacts with each other.

                例如.如果我在 QLineEdit 中设置了一个值,则滑块将被更新,或者如果我将滑块滑过,它将更新 QLineEdit 中的值

                Eg. If I set a value in the QLineEdit, the slider will be updated, or if I slide the slider across, it will updates the value in QLineEdit

                # If user change value on the slider
                self.timer_slider.valueChanged.connect(self.set_value)
                # If user sets a value in the text box instead
                self.timer_value.textChanged.connect(self.set_slider)
                
                def set_slider(self, value):
                    self.timer_slider.setValue(int(value))
                
                def set_value(self, value):
                    self.timer_value.setText(str(value))
                

                无论如何我可以使用 float 代替 int 值吗?

                Is there anyway that I can use float instead of int values?

                推荐答案

                @dissidia 的回答很好.但是,如果您的应用程序中有很多滑块,或者如果您需要几个不同的比例因子,则将 QSlider 子类化以制作您自己的 QDoubleSlider 是值得的.

                @dissidia's answer is good. But if you have a lot of sliders in your app or if you need several different scale factors, it pays to subclass QSlider to make your own QDoubleSlider.

                下面的类基于其他人的工作,但如果你链接到 QLineEdit 或 QDoubleSpinBox,你会想要一个额外的功能:一个新的 valueChanged 信号,称为 doubleValueChanged.

                The class below is based on the work of others, but has an extra feature you'll want if you link to a QLineEdit or QDoubleSpinBox: a new signal for valueChanged called doubleValueChanged.

                class DoubleSlider(QSlider):
                
                    # create our our signal that we can connect to if necessary
                    doubleValueChanged = pyqtSignal(float)
                
                    def __init__(self, decimals=3, *args, **kargs):
                        super(DoubleSlider, self).__init__( *args, **kargs)
                        self._multi = 10 ** decimals
                
                        self.valueChanged.connect(self.emitDoubleValueChanged)
                
                    def emitDoubleValueChanged(self):
                        value = float(super(DoubleSlider, self).value())/self._multi
                        self.doubleValueChanged.emit(value)
                
                    def value(self):
                        return float(super(DoubleSlider, self).value()) / self._multi
                
                    def setMinimum(self, value):
                        return super(DoubleSlider, self).setMinimum(value * self._multi)
                
                    def setMaximum(self, value):
                        return super(DoubleSlider, self).setMaximum(value * self._multi)
                
                    def setSingleStep(self, value):
                        return super(DoubleSlider, self).setSingleStep(value * self._multi)
                
                    def singleStep(self):
                        return float(super(DoubleSlider, self).singleStep()) / self._multi
                
                    def setValue(self, value):
                        super(DoubleSlider, self).setValue(int(value * self._multi))
                

                这篇关于对 QSlider 使用浮点数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:如何将绘图作为 PyQt5 小部件? 下一篇:如何制作一个可以在 Qt 中将其行折叠成类别的表

                相关文章

                最新文章

              • <i id='loc9q'><tr id='loc9q'><dt id='loc9q'><q id='loc9q'><span id='loc9q'><b id='loc9q'><form id='loc9q'><ins id='loc9q'></ins><ul id='loc9q'></ul><sub id='loc9q'></sub></form><legend id='loc9q'></legend><bdo id='loc9q'><pre id='loc9q'><center id='loc9q'></center></pre></bdo></b><th id='loc9q'></th></span></q></dt></tr></i><div id='loc9q'><tfoot id='loc9q'></tfoot><dl id='loc9q'><fieldset id='loc9q'></fieldset></dl></div>

                  <small id='loc9q'></small><noframes id='loc9q'>

                    • <bdo id='loc9q'></bdo><ul id='loc9q'></ul>
                    <tfoot id='loc9q'></tfoot>
                    <legend id='loc9q'><style id='loc9q'><dir id='loc9q'><q id='loc9q'></q></dir></style></legend>