如何获取哪个QradioButton调用SLOT

How to get which QradioButton invoke the SLOT

本文关键字:QradioButton 调用 SLOT 何获取 获取      更新时间:2023-10-16

我创建了几个QradioButton并连接到同一个SLOT。在插槽中,我想知道哪个QradioButton调用插槽并执行相关操作。我发现有一种方法可以使用qobject_cast和qobject::sender(),但似乎不起作用。这是代码:

头文件:

class dialoginput : public QDialog
{
    Q_OBJECT
public:
    dialoginput(QWidget *parent = 0);
    QRadioButton *radio1;
    QRadioButton *radio2;
    QRadioButton *radio3;
private slots:
    void setText_2();
private:
    QLabel *label_0_0;
    QLabel *label_1;
};

主文件:

dialoginput::dialoginput(QWidget *parent): QDialog(parent){
    label_0_0 = new QLabel("label_1:");
    label_1 = new QLabel;  
    QWidget *window = new QWidget;
    QVBoxLayout *windowLayout = new QVBoxLayout;
    QGroupBox *box = new QGroupBox("Display Type");
    radio1 = new QRadioButton("3");
    radio2 = new QRadioButton("5");
    radio3 = new QRadioButton("9");
    QVBoxLayout *radioLayout = new QVBoxLayout;
    connect(radio1,SIGNAL(clicked()),this,SLOT(setText_2()));
    connect(radio2,SIGNAL(clicked()),this,SLOT(setText_2()));
    connect(radio3,SIGNAL(clicked()),this,SLOT(setText_2()));
    radioLayout->addWidget(radio1);
    radioLayout->addWidget(radio2);
    radioLayout->addWidget(radio3);
    box->setLayout(radioLayout);
    windowLayout->addWidget(box);
    windowLayout->addWidget(label_0_0);
    windowLayout->addWidget(label_1);
    window->setLayout(windowLayout);
    window->show();
}
void dialoginput::setText_2(){
    QObject *object = QObject::sender();
    QRadioButton* pbtn = qobject_cast<QRadioButton*>(object);
    QString name = pbtn->objectName();
    label_1->setText(name);
    if(!QString::compare(name, "3")){       
    }
    else if(!QString::compare(name, "5")){
    }
    else if(!QString::compare(name, "9")){
    }
}
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    dialoginput *input = new dialoginput();
    return a.exec();
}

尽管使用sender()方法可以解决您的问题,但我不建议使用它。问题是,信号和插槽的设计是为了分离发射器和接收器。接收器不需要知道哪些对象,甚至什么类型的对象可以触发其插槽。当您使用sender()时,您依赖于这样一个事实,即接收器了解触发其插槽的所有对象。如果这种情况在未来发生变化怎么办?

你应该看看QSignalMapper,它是专门为这种需求而设计的。文档中有很好的例子。

您可以为每个单选按钮创建单独的包装器槽,然后将信息传递给要调用的函数。类似这样的东西:-

class dialoginput : public QDialog
{
    Q_OBJECT
public:
    QRadioButton *radio1;
    QRadioButton *radio2;
    QRadioButton *radio3;
private slots:
   void Radio1Selected() { setText_2(1); }
   void Radio2Selected() { setText_2(2); }
   void Radio3Selected() { setText_2(3); }
private:
   void setText_2(int id);

};

然后连接每个单选按钮:-

connect(radio1,SIGNAL(clicked()),this,SLOT(Radio1Selected()));
connect(radio2,SIGNAL(clicked()),this,SLOT(Radio2Selected()));
connect(radio3,SIGNAL(clicked()),this,SLOT(Radio3Selected()));

现在,当调用setText_2时,id将表示所选的单选按钮。

您在setText_2()上正确地获取了sender Object,但没有设置radio1、radio2和radio3的objectName属性。请使用"setObjectName()"API。

为单选按钮编写单参数自定义信号,然后发射。在slot.check中捕获该参数。检查对应的单选按钮

您还可以创建QButtonGroup并使用lambda表达式(c++11)

class dialoginput : public QDialog
{
    Q_OBJECT
public:
private:
   void setText_2(int id);
   QRadioButton *radio1;
   QRadioButton *radio2;
   QRadioButton *radio3;
   QButtonGroup _btnGroup; 
};

将3 QRadioButton添加到QButtonGroup 后

_btnGroup.addButton(radio1, 1);
_btnGroup.addButton(radio2, 2);
_btnGroup.addButton(radio3, 3);
connect(&_btnGroup, static_cast<void(QButtonGroup::*)(int)>(&QButtonGroup::buttonClicked), [=](int id){
            setText_2(id);});