What does QObject::connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept())); do in following code



  • QDialogButtonBox* buttonBox = new QDialogButtonBox();
    buttonBox->setStandardButtons(QDialogButtonBox::Apply | QDialogButtonBox::Close);
    QPushButton* applyButton = buttonBox->button(QDialogButtonBox::Apply);
    QObject::connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));



  • Hi, friend. Welcome.
    The Qt manual said:

    [signal] void QDialogButtonBox::accepted()
    This signal is emitted when a button inside the button box is clicked, as long as it was defined with the AcceptRole or YesRole.

    QDialogButtonBox include more than one button. If there is a AcceptRole button clicked and the signal will be emitted. From the signal, we can know AcceptRole button is clicked in QDialogButtonBox.

    eg:

    #include "widget.h"
    #include "ui_widget.h"
    
    #include <QDialogButtonBox>
    #include <QDebug>
    
    Widget::Widget(QWidget *parent) :
        QWidget(parent),
        ui(new Ui::Widget)
    {
        ui->setupUi(this);
    
        QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Save | QDialogButtonBox::Cancel, this);
        ///< Ok and Save is AcceptRole
        connect(buttonBox, &QDialogButtonBox::accepted, this, [=](){ qDebug("ok or save");     });
        connect(buttonBox, &QDialogButtonBox::rejected, this, [=](){ qDebug("cancel"); });
    }
    
    Widget::~Widget()
    {
        delete ui;
    }
    
    

    Add the button roles: enum QDialogButtonBox::StandardButton
    eg:

    QDialogButtonBox::Ok An "OK" button defined with the AcceptRole.
    QDialogButtonBox::Open An "Open" button defined with the AcceptRole.
    QDialogButtonBox::Save A "Save" button defined with the AcceptRole.
    QDialogButtonBox::Cancel A "Cancel" button defined with the RejectRole.
    QDialogButtonBox::Close A "Close" button defined with the RejectRole.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.