Sagar Mohantu
Sagar Mohantu

Reputation: 121

How to call a function when QDialog Cancel or OK is pressed?

I am doing 3 tabs from QDialog. Then adding 2 widget and 1 QDialog to 3 tabs. I have called the QDialog::accept and QDialog::reject. But I want custom methods to be called and on calling them should reset the dialog screen to list box selected screen. My code follows:

BTSettingsTab::BTSettingsTab(const QFileInfo &fileInfo, QWidget *parent)
    : QDialog(parent)
{
...........
.............
QLabel *topLabel = new QLabel(tr("Choose setting :"));

    QListWidget *settingsListBox = new QListWidget;
    QStringList settings;

    /*for (int i = 1; i <= 30; ++i)
        applications.append(tr("Application %1").arg(i));
    applicationsListBox->insertItems(0, applications);*/

    settings.append(tr("newConfiguration:"));
    settings.append(tr("Edit Configuration:"));
    settings.append(tr("Delete Configuration:"));
    settings.append(tr("add current location to  turnoff Places:"));
    settings.append(tr("temporarily turnoff distance:"));
    settings.append(tr("temporarily turn off turnoff places:"));
    settings.append(tr("factory reset:"));
    settings.append(tr("turn on distance:"));
    settings.append(tr("turn on turnoff places:"));
    settings.append(tr("exit"));
    settingsListBox->insertItems(0,settings);
QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok
                                             | QDialogButtonBox::Cancel);

            connect(buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
            connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
QVBoxLayout *layout = new QVBoxLayout;
 layout->addWidget(topLabel);
    layout->addWidget(settingsListBox);
layout->addWidget(buttonBox);
    setLayout(layout);
}

I saw your post. Thanks. I impleted the overloaded functions and breakpoint hits them. I got the row number of selected item.I used QString * curr = settingsListBox->currentItem()->text();

Now how do I start a new window?

Upvotes: 4

Views: 3728

Answers (1)

k.v.
k.v.

Reputation: 1223

Methods QDialog::accept and QDialog::reject are virtual, so they can be overriden in your custom dialog (in your case - BTSettingsTab which inherits from QDialog).

If you want to substitute behavior on accept and reject - just implement your desired behavior in the overriden methods. Or you can implement your own methods BTSettingsTab::_OnAccept() and _OnReject() and connect buttons to them instead of connecting them to QDialog::accept and reject.

If you want to add your custom behavior to default QDialog's behavior - call QDialog::accept() and QDialog::reject respectively in the end of the overloaded versions.

Upvotes: 5

Related Questions