PyQt4:QSpinbox 和 QSlider 之间的同步
我在 QSpinbox 和 QSlider 之间建立了关系。
QSpinbox 的范围为 -10.0 到 10.0,QSlider 的范围为 -100 到 100。
因此,QSlider 的值除以 10 就连接到 QSpinbox,反之亦然。
我互相使用“valueChanged()”信号。
我想在 QSpinbox 中输入“3.5”,在这种情况下,当我只输入“3”时,QSpinbox 的“valueChanged”会更改 QSlider 的值,然后 QSlider 会再次执行此操作。所以,QSpinbox 失去了焦点。
我无法一口气输入“3.5”。
“valueChanged()”SIGNAL 工作得太勤奋了:)
我该如何解决这个问题?
I make a relation between QSpinbox and QSlider.
QSpinbox has a range -10.0 to 10.0, and QSlider has a range -100 to 100.
So, the value of QSlider divided by 10 is connect to QSpinbox, and vice versa.
I use "valueChanged()" SIGNAL to each other.
And I want to type "3.5" in QSpinbox, in this case when I type just "3", QSpinbox's "valueChanged" change QSlider's value, and QSlider do again. So, QSpinbox lose it's focus.
I can't type "3.5" in one shot.
"valueChanged()" SIGNAL works too diligently :)
How can I solve this issue?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
valueChanged()
信号确实意味着在更改时主动触发。如果您希望在编辑完成时触发信号,则有一个专门用于名为editingFinished
的信号:http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qabstractspinbox.html#editingFinished您可能忽略了它,因为它是超类 QAbstractSpinBox 的成员。请注意,一旦小部件失去焦点或用户按 Enter 键,此信号就会触发。如果这也不是您想要的行为,那么唯一剩下的选择是使用具有短暂延迟的 QTimer,每次
valueChanged
触发时都会重新启动,并且一旦用户等待足够长的时间,超时实际上就会更新你的滑块。我还注意到您试图在 QSpinBox 中使用非整数值,它是为整数设计的。您可能想尝试为浮动精度设计的 QDoubleSpinBox。
valueChange()
的表现可能会稍好一些,因为它期望的是小数。虽然我只是猜测,因为我还没有测试过它: http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qdoublespinbox.htmlThe
valueChanged()
signal is indeed meant to fire actively as it is being changed. If you want a signal fired when editing has finished, there is a signal specifically for that callededitingFinished
: http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qabstractspinbox.html#editingFinishedYou may have overlooked it because its a member of the superclass QAbstractSpinBox. Note that this signal will fire once the widget loses focus or the user hits enter. If that is not the behavior you want either, then the only remaining option is to use a QTimer with a short delay that restarts everytime
valueChanged
fires, and once the user waits long enough the timeout will actually then update your slider.I also noticed that you are trying to use non-integer values with a QSpinBox, which is designed for ints. You might want to try a QDoubleSpinBox that is designed for floating precision. The
valueChange()
might behave slightly better as its expecting the decimal. Though I am just making a guess as I haven't tested it: http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qdoublespinbox.html