默认情况下checkable是不选中的,Button默认为触发按钮(trigger button),按下去马上弹起来。选中checkable后,Button变成切换按钮(toggle button),可以有两种状态,即按下和弹起。此时该按钮可以发射toggled(bool)信号,与槽函数setVisible(bool)结合,即可用于控件交替显示。
下图中两个more按钮,上边是弹起状态,下边是按下状态:

问题:I have the follwing code where moreButton is a QPushButton. When I toggle the button, nothing happens. Shouldn’t it show or hide secondaryGroupBox and tertiaryGroupBox?
QObject::connect ( moreButton, SIGNAL ( toggled ( bool ) ), secondaryGroupBox, SLOT ( setVisible ( bool ) ) );
QObject::connect ( moreButton, SIGNAL ( toggled ( bool ) ), tertiaryGroupBox, SLOT ( setVisible ( bool ) ) );
回答:Most likely, your pushbutton is not checkable. Try moreButton->setCheckable(true), a non-checkable button never emits the toggled(bool) signal.
在Qt编程中,当一个QPushButton设置为可切换(checkable)时,它会发射toggled(bool)信号。如果发现按钮点击后没有响应,可能是因为按钮未设置为可切换。解决方案是调用setCheckable(true)使其变为togglebutton。连接信号到slot setVisible(bool)可以控制groupBox的显示和隐藏。确保检查代码中moreButton是否已设置为可切换,否则toggled(bool)信号不会被触发。
1323

被折叠的 条评论
为什么被折叠?



