What does QObject::connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept())); do in following code
-
wrote on 5 Dec 2016, 04:37 last edited by
QDialogButtonBox* buttonBox = new QDialogButtonBox();
buttonBox->setStandardButtons(QDialogButtonBox::Apply | QDialogButtonBox::Close);
QPushButton* applyButton = buttonBox->button(QDialogButtonBox::Apply);
QObject::connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept())); -
wrote on 5 Dec 2016, 05:09 last edited by joeQ 12 May 2016, 05:14
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 theAcceptRole or YesRole
.QDialogButtonBox
include more than one button. If there is aAcceptRole button
clicked and the signal will be emitted. From the signal, we can knowAcceptRole button
is clicked inQDialogButtonBox
.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.
1/2