4

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().

ekhumoro
  • 115,249
  • 20
  • 229
  • 336
AAFF
  • 89
  • 1
  • 8
  • Do you have `accept()` and `reject()` slots in your `MainWindow` ? – Nejat Nov 20 '14 at 04:22
  • no that is what i am trying to do. i added Void accept(), but it doesnt work. i am new to Qt, can you please show me how to add the SLOT thank you @Nejat – AAFF Nov 20 '14 at 04:25

1 Answers1

7

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.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
ekhumoro
  • 115,249
  • 20
  • 229
  • 336