连接QT设计器中的按钮阵列
因此,我在QT设计器GUI应用程序上有大量按钮,所有命名为LED_I,我的范围为0-191,即:LED_0,LED_1,...,LED_191。除了更改输入i外,我希望基本上会发生同一件事。因此,当单击时,LED_0会调用函数ONCLICK(0),LED_75会调用OnClick(75)等。
连接我的按钮
ui.LED_0.clicked.connect(OnClick0)
我通常会使用每个按钮使用单独的函数 。但是,这将需要191个功能,而在上面的191行将我的按钮连接到它们的功能。我敢肯定,我可以通过传递点击的按钮名称并从中获取数字来使用相同的函数,但这仍然需要191行的按钮。Clicked.Connect。有什么方法可以更有效地做到这一点?
tia
so I have a mass of buttons on a QT Designer GUI application all named LED_i where i ranges from 0-191, ie: LED_0, LED_1, ..., LED_191. I would like basically the same thing to happen when clicked except changing the input i. So LED_0 when clicked would call the function OnClick(0), LED_75 would call OnClick(75) etc etc.
I am connecting my buttons with
ui.LED_0.clicked.connect(OnClick0)
usually using a separate function for each button. However this would require 191 functions, and 191 lines like the above connecting my buttons to their functions. I'm sure I could edit it s.t. I can use the same function by passing the button name that was clicked and getting the number from it but that would still require the 191 lines of button.clicked.connect. Is there any way to do this more efficiently?
TIA
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
PYQT5并非不可能。这非常适合qbuttongroup。并且有几种方法可以处理它。
qbuttongroup使用设计器
在设计器中设置QButtongroup,请执行以下操作:
分配给按钮组< /code> - &gt;
新按钮组
之后,您将看到
butt -group
(默认名称)在对象检查器中显示。要在单击一个按钮时运行代码,您可以使用Qbuttongroup的按钮信号。它将为您提供单击的按钮的引用,从对象名称中,您可以弄清楚该怎么做。
然后
在原始帖子中的代码中Qbuttongroup
,设计师中有191个按钮。这是很多按钮要安排。如果出于某种原因,您想在代码中执行此操作,则可以将每个按钮分配给组,然后将其添加到组,然后可以使用IDClickClickick Signal:
然后
It's not impossible with PyQt5. This is a good fit for a QButtonGroup. And there are a few ways to approach it.
QButtonGroup using Designer
To set up a QButtonGroup in Designer, do the following:
Assign to button group
-->New button group
After this, you'll see
buttonGroup
(the default name) show up in the Object Inspector.To run code when one of your buttons is clicked, you can use the buttonClicked signal of QButtonGroup. It will give you a reference to the button that was clicked, and from the objectName, you can figure out what to do.
then
QButtonGroup in code
In the original post, there were 191 buttons in Designer. That is a lot of buttons to arrange. If for some reason, you wanted to do it in code instead, you could assign each button an id as it is added to the group and then you could use the idClicked signal:
then