简体   繁体   English

将QGroupBox添加到QButtonGroup

[英]Add a QGroupBox to a QButtonGroup

I have a group of 3 QRadioButtons and 2 checkable QGroupBoxes that need to all be mutually exclusive. 我有一组3个QRadioButton和2个可检查的QGroupBox ,它们必须互斥。 I like the convenience of adding my radio buttons to a QButtonGroup to automatically handle this, but I can't seem to figure out how to add the QGroupBox to the button group because it doesn't inherit from QAbstractButton and I can't find access to its checkbox. 我喜欢将单选按钮添加到QButtonGroup以便自动处理的便利,但是我似乎无法弄清楚如何将QGroupBox添加到按钮组,因为它不继承自QAbstractButton并且无法找到访问权限到其复选框。

For example, 例如,

QRadioButton* rb1 = new QRadioButton("Button1");
QRadioButton* rb2 = new QRadioButton("Button2");
QRadioButton* rb3 = new QRadioButton("Button3");
QGroupBox* gb1 = new QGroupBox;
gb1->setCheckable(true);
QGroupBox* gb2 = new QGroupBox;
gb2->setCheckable(true);
QRadioButton* rb1 = new QRadioButton("Button1");

QButtonGroup* grp = new QButtonGroup;
grp->addButton(rb1);
grp->addButton(rb2);
grp->addButton(rb3);
grp->addButton(gb1);   //these two fail
grp->addButton(gb2);

Is there a simple way to accomplish this? 有没有简单的方法可以做到这一点? I know that I can connect to QGroupBox's clicked() signal, but I rather do this more cleanly than that. 我知道我可以连接到QGroupBox的clicked()信号,但是我做的比这更干净。

You can only add QAbstractButton derived classes to a QButtonGroup instance. 您只能将QAbstractButton派生的类添加到QButtonGroup实例。 So in your case if you want to handle the a QGroupBox as one of the exclusive items, I think you need to implement it by yourself. 因此,在您的情况下,如果您想将QGroupBox作为独占项之一处理,我认为您需要自己实现它。 Maybe you could connect all SIGNALs from the mentioned widget to the same SLOT, and in that SLOT you can update the checked/unchecked widgets with the help of the sender function: 也许您可以将所有信号从提到的小部件连接到同一个SLOT,在该SLOT中,您可以借助sender函数来更新已选中/未选中的小部件:

QObject* object = sender();

if ( object == groupBox1 )
{
    // ...
}
else if ( object == groupBox1 )
{
    // ...

But if you need to do this in many places then you shall implement some kind of helper class for this purpose. 但是,如果您需要在许多地方执行此操作,则应为此目的实现某种帮助程序类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM