将用户交互与程序更改分开:PyQt、QComboBox
我的 PyQt4/Python3 GUI 中有几个 QComboBox,它们在初始化期间填充了数据库中的一些条目。初始 CurrentIndex 设置为 0。还有一个复选框可以更改组合框中项目的语言。为了保留当前用户选择,我备份当前项目的索引,并在用翻译的项目填充 ComboBox 后将 CurrentIndex 设置为该数字。所有这些操作都会发出 currentIndexChanged 信号。
根据 QComboBoxes 中选择的项目,显示一些图。这个想法是在线重绘绘图 - 一旦用户更改任何 ComboBox 当前项目。在这里我遇到了一个问题,因为如果每次发出 currentIndexChanged 信号时都重新绘制绘图,那么在初始化期间以及如果翻译复选框选择发生更改,我也会重新绘制几次。
区分这些情况的最佳方法是什么?原则上,我需要将编程的当前索引更改与用户分开,并且仅在后面的情况下更新绘图(在 GUI 初始化期间,我可以以编程方式调用更新绘图函数一次)。我应该写入/重写任何信号吗?如果是这样,我以前从未这样做过,并且欢迎任何提示或好的例子。使用其他信号?或者也许有办法暂时阻止所有信号?
I have several QComboBoxes in my PyQt4/Python3 GUI and they are filled with some entries from a database during the initialisation. Initial CurrentIndex is set to 0. There is also a tick box which changes the language of the items in my combo boxes. To preserve current user selection I backup index of the current item and setCurrentIndex to this number after I fill in ComboBox with translated items. All those actions emit currentIndexChanged signal.
Based on the items selected in QComboBoxes some plot is displayed. The idea is to redraw the plot online - as soon as the user changes any of ComboBox current item. And here I have a problem since if I redraw the plot every time signal currentIndexChanged is emited, I redraw it also several times during initialization and if the translation tick box selection was changed.
What is the best way to separate these cases? In principle I need to separate programmical current Index Change from the user, and update the plot only in the later case (during GUI initialisation I can programically call update plot function once). Should I write/rewrite any signal? If so, I never did that before and would welcome any hint or a good example. Use another signal? Or maybe there is a way to temporary block all signals?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以尝试一些不同的事情。
首先,您可以确保在连接信号之前完成所有初始化。
其次,您可以使用 activated 信号,该信号仅在以下情况下发送: 用户选择一个项目。 (但请注意,与 currentIndexChanged 不同,此信号是发送的即使索引没有改变)。
第三,您可以使用 blockSignals 暂时停止发送任何信号当前索引正在以编程方式更改。
这是演示这些可能性的脚本:
There are a few different things you can try.
Firstly, you can make sure you do all your initialization before you connect up the signals.
Secondly, you could use the activated signal, which is only sent whenever the user selects an item. (But note that, unlike currentIndexChanged, this signal is sent even if the index hasn't changed).
Thirdly, you could use blockSignals to temporarily stop any signals being sent while the current index is being changed programmatically.
Here's a script that demonstrates these possibilities: