Qt Radio Button 仅在实际选中按钮时调用 SLOT()

Qt Radio Button only call SLOT() when button is actually checked

我有一组单选按钮。这些都连接到同一个 SLOT(updateImage()).

它们的设置方式如下:

connect(m_radio1, SIGNAL(toggled(bool)), this, SLOT(updateImage());
connect(m_radio2, SIGNAL(toggled(bool)), this, SLOT(updateImage());
connect(m_radio3, SIGNAL(toggled(bool)), this, SLOT(updateImage());

将单选按钮从一个更改为下一个时,此 SLOT() 被调用两次,一次用于取消选择先前选择的单选按钮,一次用于选择单击的单选按钮。

我想知道,有没有办法修改我的 SLOT() 以仅在其中一种情况下发生,即当单击的项目被选中时?

谢谢

您要求连接到 toggled 信号 - 只要对象被切换,您就会被调用。有什么好惊讶的?

您可以连接到 "clicked" 信号,它提供了一个您可以测试的 "checked" 参数。

顺便说一句;如果使用现代 Qt 版本,您应该放弃 SIGNAL 和 SLOTS 宏,而是使用在编译时检查的新 connect() 语法。

因为你的信号有一个 bool 参数。

toggled(bool)

您可以将该参数添加到您的 SLOT method.Then 检查该参数是否被选中。

(作为补充:如何在 Python 中这样做?)假设您有一个单选按钮 radio_button 并且您希望在传递 _my_slot 时将其连接到插槽 _my_slot =13=]作为参数,使得radio_button只在被选中后发送一个信号,那么你需要添加以下内容:

radio_button.clicked.connect(lambda: _my_slot(args))