4

我创建了几个 QradioButton 并连接到同一个 SLOT。在 slot 中,我想知道哪个 QradioButton 调用 slot 并执行相关操作。我发现有一种方法可以使用 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();
}
4

5 回答 5

5

即使使用该sender()方法可以解决您的问题,我也不建议使用它。问题是,信号和插槽被设计成将发射器和接收器分开。接收者不需要知道哪些对象,甚至哪些类型的对象可以触发它的槽。当您使用 时sender(),您依赖于接收器了解触发其插槽的所有对象的事实。如果这在未来发生变化怎么办?

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

于 2013-08-13T06:10:57.080 回答
2

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

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 将代表选定的单选按钮。

于 2013-08-13T07:38:12.810 回答
1

您在 setText_2() 上正确获取了发送者对象,但您没有设置 radio1、radio2 和 radio3 的 objectName 属性。请使用“setObjectName( )”API。

于 2013-08-13T04:25:43.190 回答
0

为单选按钮编写单个参数自定义信号,然后发出它。在 slot.catch 中捕获该参数。检查相应的单选按钮

于 2013-08-13T03:48:49.320 回答
0

您还可以创建一个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);});
于 2018-08-03T09:57:35.600 回答