连接QT设计器中的按钮阵列

发布于 2025-01-22 08:59:49 字数 385 浏览 5 评论 0原文

因此,我在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倥絔 2025-01-29 08:59:50

PYQT5并非不可能。这非常适合qbuttongroup。并且有几种方法可以处理它。

qbuttongroup使用设计器

在设计器中设置QButtongroup,请执行以下操作:

  • 选择两个或多个按钮,radioboxes或复选框
  • 右键单击其中一个按钮,您将获得上下文菜单项,分配给按钮组< /code> - &gt; 新按钮组

之后,您将看到butt -group(默认名称)在对象检查器中显示。

要在单击一个按钮时运行代码,您可以使用Qbuttongroup的按钮信号。它将为您提供单击的按钮的引用,从对象名称中,您可以弄清楚该怎么做。

ui.buttonGroup.buttonClicked.connect(self.OnClicked)

然后

def OnClicked(self, button):
     # button objectName follows pattern LED_<number>
     button_number = int(button.objectName()[4:])
     ... do stuff here with button_number

在原始帖子中的代码中Qbuttongroup

,设计师中有191个按钮。这是很多按钮要安排。如果出于某种原因,您想在代码中执行此操作,则可以将每个按钮分配给组,然后将其添加到组,然后可以使用IDClickClickick Signal:

grid = QGridLayout()
buttonGroup = QButtonGroup()
buttonGroup.idClicked.connect(OnClick)
buttonList = []
for row in range(14):
    rowList = []
    for col in range(14):
        button_number = 14*row + col
        button = QPushButton(f'{button_number}', objectName=f'LED_{button_number}')
        rowList.append(button)
        buttonGroup.addButton(button, button_number)
        grid.addWidget(button, row, col)

然后

def OnClick(self, idClicked):
    ... do something with idClicked here


    

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:

  • select two or more buttons, radioboxes, or checkboxes
  • right click on one of the buttons and you'll get a context menu item, 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.

ui.buttonGroup.buttonClicked.connect(self.OnClicked)

then

def OnClicked(self, button):
     # button objectName follows pattern LED_<number>
     button_number = int(button.objectName()[4:])
     ... do stuff here with button_number

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:

grid = QGridLayout()
buttonGroup = QButtonGroup()
buttonGroup.idClicked.connect(OnClick)
buttonList = []
for row in range(14):
    rowList = []
    for col in range(14):
        button_number = 14*row + col
        button = QPushButton(f'{button_number}', objectName=f'LED_{button_number}')
        rowList.append(button)
        buttonGroup.addButton(button, button_number)
        grid.addWidget(button, row, col)

then

def OnClick(self, idClicked):
    ... do something with idClicked here


    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文