QDialogButtonBox buttons not responding

9.2k views Asked by At

When I run the following function, the dialog shows with everything in place. The problem is that the buttons won't connect. OK and Cancel do not response to mouse clicks.

void MainWindow::initializeBOX(){

        QDialog dlg;
        QVBoxLayout la(&dlg);
        QLineEdit ed;
        la.addWidget(&ed);


        //QDialogButtonBox bb(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
        //btnbox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
         QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok |     QDialogButtonBox::Cancel);

         connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
         connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));

         la.addWidget(buttonBox);
         dlg.setLayout(&la);


        if(dlg.exec() == QDialog::Accepted)
        {
            mTabWidget->setTabText(0, ed.text());
        }

      }

At runtime, an error in the cmd shows: No such slots as accept() and reject().

1

There are 1 answers

0
ekhumoro On BEST ANSWER

You are specifying the wrong receiver in the connection. It's the dialog that has the accept() and reject() slots, not the main window (i.e. this).

So, instead, you just need:

 connect(buttonBox, SIGNAL(accepted()), &dlg, SLOT(accept()));
 connect(buttonBox, SIGNAL(rejected()), &dlg, SLOT(reject()));

And now when you click the buttons, the dialog will close, and exec() will return either QDialog::Accepted for OK, or QDialog::Rejected for Cancel.